1. Gastos (cálculos antiguos)

Gastos_casa %>% 
  dplyr::select(-Tiempo,-link) %>%
  dplyr::select(fecha, gasto, monto, gastador,obs) %>% tail(30) %>% 
  knitr::kable(format = "markdown", size=12)
fecha gasto monto gastador obs
9/3/2023 Forro cortina ducha 2490 Tami NA
4/3/2023 Microondas regalo 40000 Tami NA
9/3/2023 Comida 106490 Tami Soul Bar
9/3/2023 Comida 27642 Tami NA
13/3/2023 Comida 51473 Tami NA
13/3/2023 Diosi 20990 Tami Antiparasitario
16/3/2023 Vacunas Influenza 19980 Tami NA
20/3/2023 Comida 52314 Tami NA
26/3/2023 Comida 24970 Andrés caramagnola
28/3/2023 Comida 71805 Tami NA
29/3/2023 Electricidad 42447 Andrés PAC ENEL 01686518
30/3/2023 Netflix 8320 Tami NA
31/3/2023 Comida 13226 Tami NA
31/3/2023 Comida 100000 Andrés wild foods
31/3/2023 Enceres 15400 Tami Incoludido
9/4/2023 Gas 67300 Andrés el de la derecha
10/4/2023 Comida 61792 Tami NA
17/4/2023 Comida 41602 Tami NA
19/4/2023 VTR 21990 Andrés NA
19/4/2023 nacho 55000 Andrés NA
22/4/2023 Comida 19420 Tami NA
23/4/2023 Comida 50617 Tami NA
23/4/2023 Crunchyroll 49900 Tami NA
23/4/2023 Netflix 5940 Tami NA
28/4/2023 Electricidad 43471 Andrés NA
29/4/2023 Comida 17000 Andrés pizza y dulces y nueces y almendras
30/4/2023 Comida 84066 Tami NA
30/4/2023 Parafina 38640 Tami NA
31/3/2019 Comida 9000 Andrés NA
8/9/2019 Comida 24588 Andrés Super Lider

#para ver las diferencias depués de la diosi
Gastos_casa %>%
    dplyr::mutate(fecha= lubridate::parse_date_time(fecha, c("%d/%m/%Y"),exact=T)) %>% 
    dplyr::mutate(fecha=strftime(fecha, format = "%Y-W%V")) %>%
    dplyr::mutate(gastador=ifelse(gastador=="Andrés",1,0)) %>%
    dplyr::group_by(gastador, fecha,.drop = F) %>% 
    dplyr::summarise(gasto_media=mean(monto,na.rm=T)) %>% 
    dplyr::mutate(treat=ifelse(fecha>"2019-W26",1,0)) %>%
    #dplyr::mutate(fecha_simp=lubridate::week(fecha)) %>%#después de  diosi. Junio 24, 2019 
    dplyr::mutate(gastador_nombre=plyr::revalue(as.character(gastador), c("0" = "Tami", "1"="Andrés"))) %>% 
    assign("ts_gastos_casa_week_treat", ., envir = .GlobalEnv) 

gplots::plotmeans(gasto_media ~ gastador_nombre, main="Promedio de gasto por gastador", data=ts_gastos_casa_week_treat,ylim=c(0,75000), xlab="", ylab="")

par(mfrow=c(1,2)) 
gplots::plotmeans(gasto_media ~ gastador_nombre, main="Antes de Diosi", data=ts_gastos_casa_week_treat[ts_gastos_casa_week_treat$treat==0,], xlab="", ylab="", ylim=c(0,70000))

gplots::plotmeans(gasto_media ~ gastador_nombre, main="Después de Diosi", data=ts_gastos_casa_week_treat[ts_gastos_casa_week_treat$treat==1,], xlab="", ylab="",ylim=c(0,70000))

library(ggiraph)
library(scales)
#if( requireNamespace("dplyr", quietly = TRUE)){
gg <- Gastos_casa %>%
  dplyr::mutate(fecha= lubridate::parse_date_time(fecha, c("%d/%m/%Y"),exact=T)) %>% 
  dplyr::mutate(gastador=ifelse(gastador=="Andrés",1,0)) %>%
  dplyr::mutate(fecha_simp=tsibble::yearweek(fecha)) %>%
  dplyr::mutate(fecha_week=strftime(fecha, format = "%Y-W%V")) %>%
  dplyr::mutate(treat=ifelse(fecha_week>"2019 W26",1,0)) %>%
  dplyr::mutate(gastador_nombre=plyr::revalue(as.character(gastador), c("0" = "Tami", "1"="Andrés"))) %>% 
#  dplyr::mutate(week=as.Date(as.character(lubridate::floor_date(fecha, "week"))))%>%
  #dplyr::mutate(fecha_week= lubridate::parse_date_time(fecha_week, c("%Y-W%V"),exact=T)) %>% 
  dplyr::group_by(gastador_nombre, fecha_simp) %>%
  dplyr::summarise(monto_total=sum(monto)) %>%
  dplyr::mutate(tooltip= paste0(substr(gastador_nombre,1,1),"=",round(monto_total/1000,2))) %>%
  ggplot(aes(hover_css = "fill:none;")) +#, ) +
  #stat_summary(geom = "line", fun.y = median, size = 1, alpha=0.5, aes(color="blue")) +
  geom_line(aes(x = fecha_simp, y = monto_total, color=as.factor(gastador_nombre)),size=1,alpha=.5) +
                       ggiraph::geom_point_interactive(aes(x = fecha_simp, y = monto_total, color=as.factor(gastador_nombre),tooltip=tooltip),size = 1) +
  #geom_text(aes(x = fech_ing_qrt, y = perc_dup-0.05, label = paste0(n)), vjust = -1,hjust = 0, angle=45, size=3) +
 # guides(color = F)+
  sjPlot::theme_sjplot2() +
  geom_vline(xintercept = as.Date("2019-06-24"),linetype = "dashed") +
  labs(y="Gastos (en miles)",x="Semanas y Meses", subtitle="Interlineado, incorporación de la Diosi; Azul= Tami; Rojo= Andrés") + ggtitle( "Figura 4. Gastos por Gastador") +
  scale_y_continuous(labels = f <- function(x) paste0(x/1000)) + 
  scale_color_manual(name = "Gastador", values= c("blue", "red"), labels = c("Tami", "Andrés")) +
  scale_x_yearweek(date_breaks = "1 month", minor_breaks = "1 week", labels=scales::date_format("%m/%y")) +
  theme(axis.text.x = element_text(vjust = 0.5,angle = 35), legend.position='bottom')+
     theme(
    panel.border = element_blank(), 
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(), 
    axis.line = element_line(colour = "black")
    )

#  x <- girafe(ggobj = gg)
#  x <- girafe_options(x = x,
#                      opts_hover(css = "stroke:red;fill:orange") )
#  if( interactive() ) print(x)

#}
tooltip_css <- "background-color:gray;color:white;font-style:italic;padding:10px;border-radius:10px 20px 10px 20px;"

#ggiraph(code = {print(gg)}, tooltip_extra_css = tooltip_css, tooltip_opacity = .75 )

x <- girafe(ggobj = gg)
x <- girafe_options(x,
  opts_zoom(min = 1, max = 3), opts_hover(css =tooltip_css))
x
plot<-Gastos_casa %>%
    dplyr::mutate(fecha= lubridate::parse_date_time(fecha, c("%d/%m/%Y"),exact=T)) %>% 
    dplyr::mutate(fecha_week=strftime(fecha, format = "%Y-W%V")) %>%
    dplyr::mutate(month=as.Date(as.character(lubridate::floor_date(fecha, "month"))))%>%
    dplyr::group_by(month)%>%
    dplyr::summarise(gasto_total=sum(monto)/1000) %>%
      ggplot2::ggplot(aes(x = month, y = gasto_total)) +
      geom_point()+
      geom_line(size=1) +
      sjPlot::theme_sjplot2() +
      geom_vline(xintercept = as.Date("2019-06-24"),linetype = "dashed") +
      geom_vline(xintercept = as.Date("2019-03-23"),linetype = "dashed", color="red") +
      labs(y="Gastos (en miles)",x="Meses/Año", subtitle="Interlineado, incorporación de la Diosi") + 
      ggtitle( "Figura. Suma de Gastos por Mes") +        
      scale_x_date(breaks = "1 month", minor_breaks = "1 month", labels=scales::date_format("%m/%y")) +
      theme(axis.text.x = element_text(vjust = 0.5,angle = 45)) 
plotly::ggplotly(plot)  
plot2<-Gastos_casa %>%
    dplyr::mutate(fecha= lubridate::parse_date_time(fecha, c("%d/%m/%Y"),exact=T)) %>% 
    dplyr::mutate(fecha_week=strftime(fecha, format = "%Y-W%V")) %>%
    dplyr::mutate(day=as.Date(as.character(lubridate::floor_date(fecha, "day"))))%>%
    dplyr::group_by(day)%>%
    summarise(gasto_total=sum(monto)/1000) %>%
      ggplot2::ggplot(aes(x = day, y = gasto_total)) +
      geom_line(size=1) +
      sjPlot::theme_sjplot2() +
      geom_vline(xintercept = as.Date("2019-06-24"),linetype = "dashed") +
      geom_vline(xintercept = as.Date("2020-03-23"),linetype = "dashed", color="red") +
      labs(y="Gastos (en miles)",x="Meses/Año", subtitle="Interlineado, incorporación de la Diosi") + 
      ggtitle( "Figura. Suma de Gastos por Día") +        
      scale_x_date(breaks = "1 month", minor_breaks = "1 week", labels=scales::date_format("%m/%y")) +
      theme(axis.text.x = element_text(vjust = 0.5,angle = 45)) 
plotly::ggplotly(plot2)  
tsData <- Gastos_casa %>%
    dplyr::mutate(fecha= lubridate::parse_date_time(fecha, c("%d/%m/%Y"),exact=T)) %>% 
    dplyr::mutate(fecha_week=strftime(fecha, format = "%Y-W%V")) %>%
    dplyr::mutate(day=as.Date(as.character(lubridate::floor_date(fecha, "day"))))%>%
    dplyr::group_by(day)%>%
    summarise(gasto_total=sum(monto))%>%
    dplyr::mutate(covid=case_when(day>as.Date("2019-06-02")~1,TRUE~0))%>%
    dplyr::mutate(covid=case_when(day>as.Date("2020-03-10")~covid+1,TRUE~covid))%>%
    dplyr::mutate(covid=as.factor(covid))%>%
  data.frame()
tsData_gastos <-ts(tsData$gasto_total, frequency=7)
mstsData_gastos <- forecast::msts(Gastos_casa$monto, seasonal.periods=c(7,30))
  tsData_gastos = decompose(tsData_gastos)
#plot(tsData_Santiago, title="Descomposición del número de casos confirmados para Santiago")
forecast::autoplot(tsData_gastos, main="Descomposición de los Gastos Diarios")+
    theme_bw()+ labs(x="Weeks")

tsdata_gastos_trend<-cbind(tsData,trend=as.vector(tsData_gastos$trend))%>% na.omit()
#tsData_gastos$trend
#Using the inputted variables, a Type-2 Sum Squares ANCOVA Lagged Dependent Variable model is fitted which estimates the difference in means between interrupted and non-interrupted time periods, while accounting for the lag of the dependent variable and any further specified covariates.
#Typically such analyses use Auto-regressive Integrated Moving Average (ARIMA) models to handle the serial dependence of the residuals of a linear model, which is estimated either as part of the ARIMA process or through a standard linear regression modeling process [9,17]. All such time series methods enable the effect of the event to be separated from general trends and serial dependencies in time, thereby enabling valid statistical inferences to be made about whether an intervention has had an effect on a time series.
   #it uses Type-2 Sum Squares ANCOVA Lagged Dependent Variable model
   #ITSA model da cuenta de observaciones autocorrelacionadas e impactos dinámicos mediante una regresión de deltas en rezagados. Una vez que se incorporan en el modelo, se controlan. 
#residual autocorrelation assumptions
#TSA allows the model to account for baseline levels and trends present in the data therefore allowing us to attribute significant changes to the interruption
#RDestimate(all~agecell,data=metro_region,cutpoint = 21)
tsdata_gastos_trend<-cbind(tsData,trend=as.vector(tsData_gastos$trend))%>% na.omit()

itsa_metro_region_quar2<-
        its.analysis::itsa.model(time = "day", depvar = "trend",data=tsdata_gastos_trend,
                                 interrupt_var = "covid", 
                                 alpha = 0.05,no.plots = F, bootstrap = TRUE, Reps = 10000, print = F) 

print(itsa_metro_region_quar2)
## [[1]]
## [1] "ITSA Model Fit"
## 
## $aov.result
## Anova Table (Type II tests)
## 
## Response: depvar
##                   Sum Sq  Df   F value Pr(>F)    
## interrupt_var 6.2622e+08   2    6.4912 0.0016 ** 
## lag_depvar    8.3931e+10   1 1740.0118 <2e-16 ***
## Residuals     2.7639e+10 573                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## $tukey.result
##   Tukey multiple comparisons of means
##     95% family-wise confidence level
## 
## Fit: stats::aov(formula = x$depvar ~ x$interrupt_var)
## 
## $`x$interrupt_var`
##          diff       lwr      upr     p adj
## 1-0  7228.838  1003.374 13454.30 0.0179801
## 2-0 28227.268 22538.515 33916.02 0.0000000
## 2-1 20998.430 17608.285 24388.57 0.0000000
## 
## 
## $data
##        depvar interrupt_var lag_depvar
## 2    19269.29             0   16010.00
## 3    24139.00             0   19269.29
## 4    23816.14             0   24139.00
## 5    26510.14             0   23816.14
## 6    23456.71             0   26510.14
## 7    24276.71             0   23456.71
## 8    18818.71             0   24276.71
## 9    18517.14             0   18818.71
## 10   15475.29             0   18517.14
## 11   16365.29             0   15475.29
## 12   12621.29             0   16365.29
## 13   12679.86             0   12621.29
## 14   13440.71             0   12679.86
## 15   15382.86             0   13440.71
## 16   13459.71             0   15382.86
## 17   14644.14             0   13459.71
## 18   13927.00             0   14644.14
## 19   22034.57             0   13927.00
## 20   20986.00             0   22034.57
## 21   20390.57             0   20986.00
## 22   22554.14             0   20390.57
## 23   21782.57             0   22554.14
## 24   22529.57             0   21782.57
## 25   24642.71             0   22529.57
## 26   17692.29             0   24642.71
## 27   19668.29             0   17692.29
## 28   28640.00             0   19668.29
## 29   28706.00             0   28640.00
## 30   28331.57             0   28706.00
## 31   25617.86             0   28331.57
## 32   27223.29             0   25617.86
## 33   31622.57             0   27223.29
## 34   32021.43             0   31622.57
## 35   33634.57             0   32021.43
## 36   30784.86             0   33634.57
## 37   34770.57             0   30784.86
## 38   38443.00             1   34770.57
## 39   35073.00             1   38443.00
## 40   31422.29             1   35073.00
## 41   30103.29             1   31422.29
## 42   19319.29             1   30103.29
## 43   27926.29             1   19319.29
## 44   30715.43             1   27926.29
## 45   31962.29             1   30715.43
## 46   39790.14             1   31962.29
## 47   39211.57             1   39790.14
## 48   44548.57             1   39211.57
## 49   49398.00             1   44548.57
## 50   41039.00             1   49398.00
## 51   34821.29             1   41039.00
## 52   29123.57             1   34821.29
## 53   21275.71             1   29123.57
## 54   28476.14             1   21275.71
## 55   24561.86             1   28476.14
## 56   20323.57             1   24561.86
## 57   25370.00             1   20323.57
## 58   26811.86             1   25370.00
## 59   27151.86             1   26811.86
## 60   27623.29             1   27151.86
## 61   22896.57             1   27623.29
## 62   41889.29             1   22896.57
## 63   44000.14             1   41889.29
## 64   38558.00             1   44000.14
## 65   43373.86             1   38558.00
## 66   49001.00             1   43373.86
## 67   61213.29             1   49001.00
## 68   58939.57             1   61213.29
## 69   42046.86             1   58939.57
## 70   39191.71             1   42046.86
## 71   42646.43             1   39191.71
## 72   36121.57             1   42646.43
## 73   30915.57             1   36121.57
## 74   20273.43             1   30915.57
## 75   23938.29             1   20273.43
## 76   19274.29             1   23938.29
## 77   21662.29             1   19274.29
## 78   15819.00             1   21662.29
## 79   18126.14             1   15819.00
## 80   17240.71             1   18126.14
## 81   16127.71             1   17240.71
## 82   13917.14             1   16127.71
## 83   15379.86             1   13917.14
## 84   19510.14             1   15379.86
## 85   24567.29             1   19510.14
## 86   25700.43             1   24567.29
## 87   25729.00             1   25700.43
## 88   26435.00             1   25729.00
## 89   31157.14             1   26435.00
## 90   29818.43             1   31157.14
## 91   30962.43             1   29818.43
## 92   28746.71             1   30962.43
## 93   27830.71             1   28746.71
## 94   28252.14             1   27830.71
## 95   28717.57             1   28252.14
## 96   21365.43             1   28717.57
## 97   24816.86             1   21365.43
## 98   16838.57             1   24816.86
## 99   15529.14             1   16838.57
## 100  13286.29             1   15529.14
## 101  13629.43             1   13286.29
## 102  14404.86             1   13629.43
## 103  19524.86             1   14404.86
## 104  18475.71             1   19524.86
## 105  22495.00             1   18475.71
## 106  22254.57             1   22495.00
## 107  24173.29             1   22254.57
## 108  27466.43             1   24173.29
## 109  24602.43             1   27466.43
## 110  20531.14             1   24602.43
## 111  20846.43             1   20531.14
## 112  23875.71             1   20846.43
## 113  36312.71             1   23875.71
## 114  34244.00             1   36312.71
## 115  36347.43             1   34244.00
## 116  39779.71             1   36347.43
## 117  42018.71             1   39779.71
## 118  39372.57             1   42018.71
## 119  33444.00             1   39372.57
## 120  29255.86             1   33444.00
## 121  31640.14             1   29255.86
## 122  29671.14             1   31640.14
## 123  31023.71             1   29671.14
## 124  39723.43             1   31023.71
## 125  39314.14             1   39723.43
## 126  38239.86             1   39314.14
## 127  34649.43             1   38239.86
## 128  36688.43             1   34649.43
## 129  42867.57             1   36688.43
## 130  42226.86             1   42867.57
## 131  32155.14             1   42226.86
## 132  33603.00             1   32155.14
## 133  37254.43             1   33603.00
## 134  33145.57             1   37254.43
## 135  31299.43             1   33145.57
## 136  30252.00             1   31299.43
## 137  26310.71             1   30252.00
## 138  27929.86             1   26310.71
## 139  27666.14             1   27929.86
## 140  25017.57             1   27666.14
## 141  27335.00             1   25017.57
## 142  25760.71             1   27335.00
## 143  18436.86             1   25760.71
## 144  21906.00             1   18436.86
## 145  19418.14             1   21906.00
## 146  22826.14             1   19418.14
## 147  23444.29             1   22826.14
## 148  25264.86             1   23444.29
## 149  25473.29             1   25264.86
## 150  27366.86             1   25473.29
## 151  28855.86             1   27366.86
## 152  32326.86             1   28855.86
## 153  27141.43             1   32326.86
## 154  26297.71             1   27141.43
## 155  23499.14             1   26297.71
## 156  30246.29             1   23499.14
## 157  39931.86             1   30246.29
## 158  38020.43             2   39931.86
## 159  35004.00             2   38020.43
## 160  40750.86             2   35004.00
## 161  42363.29             2   40750.86
## 162  46273.57             2   42363.29
## 163  41083.29             2   46273.57
## 164  35711.29             2   41083.29
## 165  41921.71             2   35711.29
## 166  60583.29             2   41921.71
## 167  63115.57             2   60583.29
## 168  61300.14             2   63115.57
## 169  57666.43             2   61300.14
## 170  55834.00             2   57666.43
## 171  58927.71             2   55834.00
## 172  57810.57             2   58927.71
## 173  48987.14             2   57810.57
## 174  52219.29             2   48987.14
## 175  56503.57             2   52219.29
## 176  56545.00             2   56503.57
## 177  64705.57             2   56545.00
## 178  53833.29             2   64705.57
## 179  50114.00             2   53833.29
## 180  39592.43             2   50114.00
## 181  29907.29             2   39592.43
## 182  33923.29             2   29907.29
## 183  45489.00             2   33923.29
## 184  44866.29             2   45489.00
## 185  51680.57             2   44866.29
## 186  58257.00             2   51680.57
## 187  70600.57             2   58257.00
## 188  76648.00             2   70600.57
## 189  69430.14             2   76648.00
## 190  69651.57             2   69430.14
## 191  77745.14             2   69651.57
## 192  72795.86             2   77745.14
## 193  67670.71             2   72795.86
## 194  55357.86             2   67670.71
## 195  48524.00             2   55357.86
## 196  50154.43             2   48524.00
## 197  45111.57             2   50154.43
## 198  36147.00             2   45111.57
## 199  43501.57             2   36147.00
## 200  41472.43             2   43501.57
## 201  41058.00             2   41472.43
## 202  41605.57             2   41058.00
## 203  49382.86             2   41605.57
## 204  59558.57             2   49382.86
## 205  59134.57             2   59558.57
## 206  61109.00             2   59134.57
## 207  63004.43             2   61109.00
## 208  67344.29             2   63004.43
## 209  78180.86             2   67344.29
## 210  69117.86             2   78180.86
## 211  55597.57             2   69117.86
## 212  49426.14             2   55597.57
## 213  39119.43             2   49426.14
## 214  35636.86             2   39119.43
## 215  39201.14             2   35636.86
## 216  27777.00             2   39201.14
## 217  47207.00             2   27777.00
## 218  55587.29             2   47207.00
## 219  56619.71             2   55587.29
## 220  82679.86             2   56619.71
## 221  91259.57             2   82679.86
## 222  93552.71             2   91259.57
## 223 102242.71             2   93552.71
## 224  91884.00             2  102242.71
## 225  85013.86             2   91884.00
## 226  84535.29             2   85013.86
## 227  80700.43             2   84535.29
## 228  79740.57             2   80700.43
## 229  85163.14             2   79740.57
## 230  86724.86             2   85163.14
## 231  80355.00             2   86724.86
## 232  74875.14             2   80355.00
## 233  81347.00             2   74875.14
## 234  66062.43             2   81347.00
## 235  56946.43             2   66062.43
## 236  47732.14             2   56946.43
## 237  38129.71             2   47732.14
## 238  42928.29             2   38129.71
## 239  45392.57             2   42928.29
## 240  37895.43             2   45392.57
## 241  30660.29             2   37895.43
## 242  42430.86             2   30660.29
## 243  35845.14             2   42430.86
## 244  40350.43             2   35845.14
## 245  31494.71             2   40350.43
## 246  30013.29             2   31494.71
## 247  34197.57             2   30013.29
## 248  37430.14             2   34197.57
## 249  26932.43             2   37430.14
## 250  33729.86             2   26932.43
## 251  38081.43             2   33729.86
## 252  44028.00             2   38081.43
## 253  47139.71             2   44028.00
## 254  46558.86             2   47139.71
## 255  58350.57             2   46558.86
## 256  78380.00             2   58350.57
## 257  78168.29             2   78380.00
## 258  70510.86             2   78168.29
## 259  72207.14             2   70510.86
## 260  67881.00             2   72207.14
## 261  69536.43             2   67881.00
## 262  62390.71             2   69536.43
## 263  50113.14             2   62390.71
## 264  45565.57             2   50113.14
## 265  45805.29             2   45565.57
## 266  41348.57             2   45805.29
## 267  51426.86             2   41348.57
## 268  47160.57             2   51426.86
## 269  51907.43             2   47160.57
## 270  49751.43             2   51907.43
## 271  54407.43             2   49751.43
## 272  54746.29             2   54407.43
## 273  61634.57             2   54746.29
## 274  58926.43             2   61634.57
## 275  69999.29             2   58926.43
## 276  63044.86             2   69999.29
## 277  63285.29             2   63044.86
## 278  61395.43             2   63285.29
## 279  67969.43             2   61395.43
## 280  60792.57             2   67969.43
## 281  56859.14             2   60792.57
## 282  44899.43             2   56859.14
## 283  43064.14             2   44899.43
## 284  62790.29             2   43064.14
## 285  69120.71             2   62790.29
## 286  69589.43             2   69120.71
## 287  66633.29             2   69589.43
## 288  65588.57             2   66633.29
## 289  70168.57             2   65588.57
## 290  74644.71             2   70168.57
## 291  52891.00             2   74644.71
## 292  41560.57             2   52891.00
## 293  34704.86             2   41560.57
## 294  46520.00             2   34704.86
## 295  50231.00             2   46520.00
## 296  49216.71             2   50231.00
## 297  76914.86             2   49216.71
## 298  83720.71             2   76914.86
## 299  84485.00             2   83720.71
## 300  89765.00             2   84485.00
## 301  87702.86             2   89765.00
## 302  82013.86             2   87702.86
## 303  85982.43             2   82013.86
## 304  57248.43             2   85982.43
## 305  52968.43             2   57248.43
## 306  52601.86             2   52968.43
## 307  45493.29             2   52601.86
## 308  42298.86             2   45493.29
## 309  46423.71             2   42298.86
## 310  37898.00             2   46423.71
## 311  36435.14             2   37898.00
## 312  30209.57             2   36435.14
## 313  34541.86             2   30209.57
## 314  33604.71             2   34541.86
## 315  37990.71             2   33604.71
## 316  35683.43             2   37990.71
## 317  65201.86             2   35683.43
## 318  62730.57             2   65201.86
## 319  64589.14             2   62730.57
## 320  73744.86             2   64589.14
## 321  76477.71             2   73744.86
## 322 105647.43             2   76477.71
## 323 103790.29             2  105647.43
## 324  76122.29             2  103790.29
## 325  74746.14             2   76122.29
## 326  72865.71             2   74746.14
## 327  63652.57             2   72865.71
## 328  60358.29             2   63652.57
## 329  25957.14             2   60358.29
## 330  30178.43             2   25957.14
## 331  30681.57             2   30178.43
## 332  33337.29             2   30681.57
## 333  32582.71             2   33337.29
## 334  39184.43             2   32582.71
## 335  40415.71             2   39184.43
## 336  34975.43             2   40415.71
## 337  34076.14             2   34975.43
## 338  34221.14             2   34076.14
## 339  28862.57             2   34221.14
## 340  35729.86             2   28862.57
## 341  36489.29             2   35729.86
## 342  36785.14             2   36489.29
## 343  37787.71             2   36785.14
## 344  39832.14             2   37787.71
## 345  41917.86             2   39832.14
## 346  41633.57             2   41917.86
## 347  33557.00             2   41633.57
## 348  22759.57             2   33557.00
## 349  28877.86             2   22759.57
## 350  27574.00             2   28877.86
## 351  27104.71             2   27574.00
## 352  24376.14             2   27104.71
## 353  29732.29             2   24376.14
## 354  34030.00             2   29732.29
## 355  39139.71             2   34030.00
## 356  37066.57             2   39139.71
## 357  38509.29             2   37066.57
## 358  40957.29             2   38509.29
## 359  49423.00             2   40957.29
## 360  50053.29             2   49423.00
## 361  50284.14             2   50053.29
## 362  53103.86             2   50284.14
## 363  50223.00             2   53103.86
## 364  49587.14             2   50223.00
## 365  41167.71             2   49587.14
## 366  37958.71             2   41167.71
## 367  33582.29             2   37958.71
## 368  31039.43             2   33582.29
## 369  26526.57             2   31039.43
## 370  34869.43             2   26526.57
## 371  37487.43             2   34869.43
## 372  46514.43             2   37487.43
## 373  39613.43             2   46514.43
## 374  38980.57             2   39613.43
## 375  37306.14             2   38980.57
## 376  36771.29             2   37306.14
## 377  26317.00             2   36771.29
## 378  31580.71             2   26317.00
## 379  23626.57             2   31580.71
## 380  33035.71             2   23626.57
## 381  44864.57             2   33035.71
## 382  48946.14             2   44864.57
## 383  46969.57             2   48946.14
## 384  49249.57             2   46969.57
## 385  56370.14             2   49249.57
## 386  67228.71             2   56370.14
## 387  59457.29             2   67228.71
## 388  53124.71             2   59457.29
## 389  52814.14             2   53124.71
## 390  61262.00             2   52814.14
## 391  61861.14             2   61262.00
## 392  71784.71             2   61861.14
## 393  59313.29             2   71784.71
## 394  61107.00             2   59313.29
## 395  60603.43             2   61107.00
## 396  60012.57             2   60603.43
## 397  58280.43             2   60012.57
## 398  56862.71             2   58280.43
## 399  41704.43             2   56862.71
## 400  51533.00             2   41704.43
## 401  50388.71             2   51533.00
## 402  49205.29             2   50388.71
## 403  56533.29             2   49205.29
## 404  47996.14             2   56533.29
## 405  47207.57             2   47996.14
## 406  45292.00             2   47207.57
## 407  40343.43             2   45292.00
## 408  39004.86             2   40343.43
## 409  36788.43             2   39004.86
## 410  30027.57             2   36788.43
## 411  39040.14             2   30027.57
## 412  42390.14             2   39040.14
## 413  36291.14             2   42390.14
## 414  30668.29             2   36291.14
## 415  47693.00             2   30668.29
## 416  52094.43             2   47693.00
## 417  56592.57             2   52094.43
## 418  47971.43             2   56592.57
## 419  43762.43             2   47971.43
## 420  42246.71             2   43762.43
## 421  46352.43             2   42246.71
## 422  33094.86             2   46352.43
## 423  32784.86             2   33094.86
## 424  26212.43             2   32784.86
## 425  32611.57             2   26212.43
## 426  42144.86             2   32611.57
## 427  50034.86             2   42144.86
## 428  46332.00             2   50034.86
## 429  42976.29             2   46332.00
## 430  39456.29             2   42976.29
## 431  39328.29             2   39456.29
## 432  35296.14             2   39328.29
## 433  30875.43             2   35296.14
## 434  27709.00             2   30875.43
## 435  29513.29             2   27709.00
## 436  31630.43             2   29513.29
## 437  29346.14             2   31630.43
## 438  34916.86             2   29346.14
## 439  42020.86             2   34916.86
## 440  38303.00             2   42020.86
## 441  37966.43             2   38303.00
## 442  41408.14             2   37966.43
## 443  38988.14             2   41408.14
## 444  43555.29             2   38988.14
## 445  38114.00             2   43555.29
## 446  27847.86             2   38114.00
## 447  26517.00             2   27847.86
## 448  39518.29             2   26517.00
## 449  39153.71             2   39518.29
## 450  45623.14             2   39153.71
## 451  40627.43             2   45623.14
## 452  41027.71             2   40627.43
## 453  42882.86             2   41027.71
## 454  47139.43             2   42882.86
## 455  35547.57             2   47139.43
## 456  41099.00             2   35547.57
## 457  35859.57             2   41099.00
## 458  44524.57             2   35859.57
## 459  48554.29             2   44524.57
## 460  51554.29             2   48554.29
## 461  47810.29             2   51554.29
## 462  50490.00             2   47810.29
## 463  50720.71             2   50490.00
## 464  52720.71             2   50720.71
## 465  52145.57             2   52720.71
## 466  55515.57             2   52145.57
## 467  52457.00             2   55515.57
## 468  58239.57             2   52457.00
## 469  50523.57             2   58239.57
## 470  47788.57             2   50523.57
## 471  46170.00             2   47788.57
## 472  42305.57             2   46170.00
## 473  46605.57             2   42305.57
## 474  55149.57             2   46605.57
## 475  48769.57             2   55149.57
## 476  50719.43             2   48769.57
## 477  44753.71             2   50719.43
## 478  42898.00             2   44753.71
## 479  46141.14             2   42898.00
## 480  34022.57             2   46141.14
## 481  26651.86             2   34022.57
## 482  28791.86             2   26651.86
## 483  31879.00             2   28791.86
## 484  33584.71             2   31879.00
## 485  34690.43             2   33584.71
## 486  27410.43             2   34690.43
## 487  41755.00             2   27410.43
## 488  49379.57             2   41755.00
## 489  57198.86             2   49379.57
## 490  51144.57             2   57198.86
## 491  56677.43             2   51144.57
## 492  65416.43             2   56677.43
## 493  69779.71             2   65416.43
## 494  54046.00             2   69779.71
## 495  43259.57             2   54046.00
## 496  40998.57             2   43259.57
## 497  41368.57             2   40998.57
## 498  42274.29             2   41368.57
## 499  35962.71             2   42274.29
## 500  38709.00             2   35962.71
## 501  44778.14             2   38709.00
## 502  51282.43             2   44778.14
## 503  52094.86             2   51282.43
## 504  52221.43             2   52094.86
## 505  45011.43             2   52221.43
## 506  46545.43             2   45011.43
## 507  42263.00             2   46545.43
## 508  45417.43             2   42263.00
## 509  45034.71             2   45417.43
## 510  37840.57             2   45034.71
## 511  39135.43             2   37840.57
## 512  38191.14             2   39135.43
## 513  39456.86             2   38191.14
## 514  42479.14             2   39456.86
## 515  34282.57             2   42479.14
## 516  28878.43             2   34282.57
## 517  56227.14             2   28878.43
## 518  65569.43             2   56227.14
## 519  69751.29             2   65569.43
## 520  62171.71             2   69751.29
## 521  63705.14             2   62171.71
## 522  79257.86             2   63705.14
## 523  87244.71             2   79257.86
## 524  58568.00             2   87244.71
## 525  52695.29             2   58568.00
## 526  48911.00             2   52695.29
## 527  53924.00             2   48911.00
## 528  53358.86             2   53924.00
## 529  42121.14             2   53358.86
## 530  47835.71             2   42121.14
## 531  62329.29             2   47835.71
## 532  56056.86             2   62329.29
## 533  59946.43             2   56056.86
## 534  64511.57             2   59946.43
## 535  61137.43             2   64511.57
## 536  55448.71             2   61137.43
## 537  47964.43             2   55448.71
## 538  46425.71             2   47964.43
## 539  55512.00             2   46425.71
## 540  55226.29             2   55512.00
## 541  46709.14             2   55226.29
## 542  49254.71             2   46709.14
## 543  49056.29             2   49254.71
## 544  49850.57             2   49056.29
## 545  39145.71             2   49850.57
## 546  29799.43             2   39145.71
## 547  34769.86             2   29799.43
## 548  44061.57             2   34769.86
## 549  43829.14             2   44061.57
## 550  45782.00             2   43829.14
## 551  38924.57             2   45782.00
## 552  49242.43             2   38924.57
## 553  50565.00             2   49242.43
## 554  38864.43             2   50565.00
## 555  49786.71             2   38864.43
## 556  58787.86             2   49786.71
## 557  58060.86             2   58787.86
## 558  62179.43             2   58060.86
## 559  57333.86             2   62179.43
## 560  70797.00             2   57333.86
## 561  89901.71             2   70797.00
## 562  78558.14             2   89901.71
## 563  65466.00             2   78558.14
## 564  70525.00             2   65466.00
## 565  68377.86             2   70525.00
## 566  69736.29             2   68377.86
## 567  60085.86             2   69736.29
## 568  41757.00             2   60085.86
## 569  49780.29             2   41757.00
## 570  56540.29             2   49780.29
## 571  57894.29             2   56540.29
## 572  60270.29             2   57894.29
## 573  61011.00             2   60270.29
## 574  57721.43             2   61011.00
## 575  71741.00             2   57721.43
## 576  59576.00             2   71741.00
## 577  52390.29             2   59576.00
## 578  61092.29             2   52390.29
## 
## $alpha
## [1] 0.05
## 
## $itsa.result
## [1] "Significant variation between time periods with chosen alpha"
## 
## $group.means
##   interrupt_var count     mean      s.d.
## 1             0    37 22066.04  6308.636
## 2             1   120 29463.10  9187.258
## 3             2   421 50461.53 15440.470
## 
## $dependent
##   [1]  19269.29  24139.00  23816.14  26510.14  23456.71  24276.71  18818.71
##   [8]  18517.14  15475.29  16365.29  12621.29  12679.86  13440.71  15382.86
##  [15]  13459.71  14644.14  13927.00  22034.57  20986.00  20390.57  22554.14
##  [22]  21782.57  22529.57  24642.71  17692.29  19668.29  28640.00  28706.00
##  [29]  28331.57  25617.86  27223.29  31622.57  32021.43  33634.57  30784.86
##  [36]  34770.57  38443.00  35073.00  31422.29  30103.29  19319.29  27926.29
##  [43]  30715.43  31962.29  39790.14  39211.57  44548.57  49398.00  41039.00
##  [50]  34821.29  29123.57  21275.71  28476.14  24561.86  20323.57  25370.00
##  [57]  26811.86  27151.86  27623.29  22896.57  41889.29  44000.14  38558.00
##  [64]  43373.86  49001.00  61213.29  58939.57  42046.86  39191.71  42646.43
##  [71]  36121.57  30915.57  20273.43  23938.29  19274.29  21662.29  15819.00
##  [78]  18126.14  17240.71  16127.71  13917.14  15379.86  19510.14  24567.29
##  [85]  25700.43  25729.00  26435.00  31157.14  29818.43  30962.43  28746.71
##  [92]  27830.71  28252.14  28717.57  21365.43  24816.86  16838.57  15529.14
##  [99]  13286.29  13629.43  14404.86  19524.86  18475.71  22495.00  22254.57
## [106]  24173.29  27466.43  24602.43  20531.14  20846.43  23875.71  36312.71
## [113]  34244.00  36347.43  39779.71  42018.71  39372.57  33444.00  29255.86
## [120]  31640.14  29671.14  31023.71  39723.43  39314.14  38239.86  34649.43
## [127]  36688.43  42867.57  42226.86  32155.14  33603.00  37254.43  33145.57
## [134]  31299.43  30252.00  26310.71  27929.86  27666.14  25017.57  27335.00
## [141]  25760.71  18436.86  21906.00  19418.14  22826.14  23444.29  25264.86
## [148]  25473.29  27366.86  28855.86  32326.86  27141.43  26297.71  23499.14
## [155]  30246.29  39931.86  38020.43  35004.00  40750.86  42363.29  46273.57
## [162]  41083.29  35711.29  41921.71  60583.29  63115.57  61300.14  57666.43
## [169]  55834.00  58927.71  57810.57  48987.14  52219.29  56503.57  56545.00
## [176]  64705.57  53833.29  50114.00  39592.43  29907.29  33923.29  45489.00
## [183]  44866.29  51680.57  58257.00  70600.57  76648.00  69430.14  69651.57
## [190]  77745.14  72795.86  67670.71  55357.86  48524.00  50154.43  45111.57
## [197]  36147.00  43501.57  41472.43  41058.00  41605.57  49382.86  59558.57
## [204]  59134.57  61109.00  63004.43  67344.29  78180.86  69117.86  55597.57
## [211]  49426.14  39119.43  35636.86  39201.14  27777.00  47207.00  55587.29
## [218]  56619.71  82679.86  91259.57  93552.71 102242.71  91884.00  85013.86
## [225]  84535.29  80700.43  79740.57  85163.14  86724.86  80355.00  74875.14
## [232]  81347.00  66062.43  56946.43  47732.14  38129.71  42928.29  45392.57
## [239]  37895.43  30660.29  42430.86  35845.14  40350.43  31494.71  30013.29
## [246]  34197.57  37430.14  26932.43  33729.86  38081.43  44028.00  47139.71
## [253]  46558.86  58350.57  78380.00  78168.29  70510.86  72207.14  67881.00
## [260]  69536.43  62390.71  50113.14  45565.57  45805.29  41348.57  51426.86
## [267]  47160.57  51907.43  49751.43  54407.43  54746.29  61634.57  58926.43
## [274]  69999.29  63044.86  63285.29  61395.43  67969.43  60792.57  56859.14
## [281]  44899.43  43064.14  62790.29  69120.71  69589.43  66633.29  65588.57
## [288]  70168.57  74644.71  52891.00  41560.57  34704.86  46520.00  50231.00
## [295]  49216.71  76914.86  83720.71  84485.00  89765.00  87702.86  82013.86
## [302]  85982.43  57248.43  52968.43  52601.86  45493.29  42298.86  46423.71
## [309]  37898.00  36435.14  30209.57  34541.86  33604.71  37990.71  35683.43
## [316]  65201.86  62730.57  64589.14  73744.86  76477.71 105647.43 103790.29
## [323]  76122.29  74746.14  72865.71  63652.57  60358.29  25957.14  30178.43
## [330]  30681.57  33337.29  32582.71  39184.43  40415.71  34975.43  34076.14
## [337]  34221.14  28862.57  35729.86  36489.29  36785.14  37787.71  39832.14
## [344]  41917.86  41633.57  33557.00  22759.57  28877.86  27574.00  27104.71
## [351]  24376.14  29732.29  34030.00  39139.71  37066.57  38509.29  40957.29
## [358]  49423.00  50053.29  50284.14  53103.86  50223.00  49587.14  41167.71
## [365]  37958.71  33582.29  31039.43  26526.57  34869.43  37487.43  46514.43
## [372]  39613.43  38980.57  37306.14  36771.29  26317.00  31580.71  23626.57
## [379]  33035.71  44864.57  48946.14  46969.57  49249.57  56370.14  67228.71
## [386]  59457.29  53124.71  52814.14  61262.00  61861.14  71784.71  59313.29
## [393]  61107.00  60603.43  60012.57  58280.43  56862.71  41704.43  51533.00
## [400]  50388.71  49205.29  56533.29  47996.14  47207.57  45292.00  40343.43
## [407]  39004.86  36788.43  30027.57  39040.14  42390.14  36291.14  30668.29
## [414]  47693.00  52094.43  56592.57  47971.43  43762.43  42246.71  46352.43
## [421]  33094.86  32784.86  26212.43  32611.57  42144.86  50034.86  46332.00
## [428]  42976.29  39456.29  39328.29  35296.14  30875.43  27709.00  29513.29
## [435]  31630.43  29346.14  34916.86  42020.86  38303.00  37966.43  41408.14
## [442]  38988.14  43555.29  38114.00  27847.86  26517.00  39518.29  39153.71
## [449]  45623.14  40627.43  41027.71  42882.86  47139.43  35547.57  41099.00
## [456]  35859.57  44524.57  48554.29  51554.29  47810.29  50490.00  50720.71
## [463]  52720.71  52145.57  55515.57  52457.00  58239.57  50523.57  47788.57
## [470]  46170.00  42305.57  46605.57  55149.57  48769.57  50719.43  44753.71
## [477]  42898.00  46141.14  34022.57  26651.86  28791.86  31879.00  33584.71
## [484]  34690.43  27410.43  41755.00  49379.57  57198.86  51144.57  56677.43
## [491]  65416.43  69779.71  54046.00  43259.57  40998.57  41368.57  42274.29
## [498]  35962.71  38709.00  44778.14  51282.43  52094.86  52221.43  45011.43
## [505]  46545.43  42263.00  45417.43  45034.71  37840.57  39135.43  38191.14
## [512]  39456.86  42479.14  34282.57  28878.43  56227.14  65569.43  69751.29
## [519]  62171.71  63705.14  79257.86  87244.71  58568.00  52695.29  48911.00
## [526]  53924.00  53358.86  42121.14  47835.71  62329.29  56056.86  59946.43
## [533]  64511.57  61137.43  55448.71  47964.43  46425.71  55512.00  55226.29
## [540]  46709.14  49254.71  49056.29  49850.57  39145.71  29799.43  34769.86
## [547]  44061.57  43829.14  45782.00  38924.57  49242.43  50565.00  38864.43
## [554]  49786.71  58787.86  58060.86  62179.43  57333.86  70797.00  89901.71
## [561]  78558.14  65466.00  70525.00  68377.86  69736.29  60085.86  41757.00
## [568]  49780.29  56540.29  57894.29  60270.29  61011.00  57721.43  71741.00
## [575]  59576.00  52390.29  61092.29
## 
## $interrupt_var
##   [1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
##  [38] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
##  [75] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
## [112] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
## [149] 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## [186] 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## [223] 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## [260] 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## [297] 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## [334] 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## [371] 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## [408] 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## [445] 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## [482] 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## [519] 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## [556] 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
## Levels: 0 1 2
## 
## $residuals
##             2             3             4             5             6 
##   1986.285232   4026.402139   -524.169904   2450.123327  -2942.141421 
##             7             8             9            10            11 
##    528.738017  -5641.157181  -1204.284715  -3984.328136   -453.494606 
##            12            13            14            15            16 
##  -4970.161346  -1661.180620   -951.173135    330.420275  -3278.822795 
##            17            18            19            20            21 
##   -424.789143  -2170.211284   6559.958384  -1527.321791  -1212.417478 
##            22            23            24            25            26 
##   1468.084123  -1181.824027    235.027186   1699.650881  -7085.334098 
##            27            28            29            30            31 
##    924.783863   8181.004354    458.077131     26.349677  -2362.298874 
##            32            33            34            35            36 
##   1599.081125   4604.590142   1184.141947   2451.011042  -1799.177213 
##            37            38            39            40            41 
##   4660.558923   4334.799332  -2223.474164  -2948.472817  -1098.050900 
##            42            43            44            45            46 
## -10736.941429   7232.348597   2549.196364   1374.617644   8119.997209 
##            47            48            49            50            51 
##    745.555153   6584.850545   6800.883172  -5768.220384  -4728.944469 
##            52            53            54            55            56 
##  -5028.657601  -7929.959326   6083.703172  -4081.741723  -4921.782064 
##            57            58            59            60            61 
##   3804.177300    864.902407    -46.867321    129.385192  -5006.606821 
##            62            63            64            65            66 
##  18089.674788   3711.724482  -3562.990405   5977.544018   7423.728854 
##            67            68            69            70            71 
##  14750.726786   1874.734984 -13044.020420  -1233.502069   4699.946962 
##            72            73            74            75            76 
##  -4824.171781  -4365.519987 -10487.996498   2415.995282  -5429.704966 
##            77            78            79            80            81 
##   1007.416017  -6909.047422    471.030854  -2417.378110  -2761.680124 
##            82            83            84            85            86 
##  -4005.984045   -624.129289   2236.279259   3707.653130    450.362184 
##            87            88            89            90            91 
##   -504.821216    176.374073   4285.592528  -1152.720328   1153.504395 
##            92            93            94            95            96 
##  -2055.390509  -1047.785191    168.882407    268.441495  -7487.770100 
##            97            98            99           100           101 
##   2346.530665  -8628.164108  -3011.125249  -4117.182498  -1826.869847 
##           102           103           104           105           106 
##  -1349.345852   3097.454299  -2396.692725   2533.421968  -1196.409288 
##           107           108           109           110           111 
##    931.036638   2558.419145  -3164.571816  -4749.433325   -899.600352 
##           112           113           114           115           116 
##   1855.965379  11663.045922  -1203.034930   2696.378723   4302.541632 
##           117           118           119           120           121 
##   3561.751729  -1028.212286  -4659.495428  -3700.660805   2319.623435 
##           122           123           124           125           126 
##  -1719.329677   1342.658393   8868.117672    906.045581    187.087348 
##           127           128           129           130           131 
##  -2470.684099   2685.399877   7094.354552   1089.125472  -8426.343175 
##           132           133           134           135           136 
##   1765.422555   4159.872409  -3119.026768  -1398.004172   -842.676358 
##           137           138           139           140           141 
##  -3874.621377   1166.207305   -503.189939  -2922.813887   1694.011371 
##           142           143           144           145           146 
##  -1892.184432  -7849.302013   1978.156375  -3521.488748   2046.381441 
##           147           148           149           150           151 
##   -294.181600    989.739911   -382.387686   1330.233377   1175.301172 
##           152           153           154           155           156 
##   3353.603671  -4845.225186  -1187.132517  -3253.220837   5923.543438 
##           157           158           159           160           161 
##   9751.482423  -3343.245263  -4700.238685   3665.375795    288.584846 
##           162           163           164           165           166 
##   2799.016709  -5786.041719  -6652.017964   4222.192323  17492.087799 
##           167           168           169           170           171 
##   3823.052663   -190.817422  -2248.440387   -926.205845   3758.358565 
##           172           173           174           175           176 
##    -44.638373  -7898.202754   2994.130878   4472.383688    794.345883 
##           177           178           179           180           181 
##   8918.950481  -9038.056719  -3318.405851 -10611.024057 -11161.708157 
##           182           183           184           185           186 
##   1262.592694   9341.756836  -1321.904361   6033.000024   6693.505081 
##           187           188           189           190           191 
##  13327.652212   8658.825621  -3809.196599   2678.522026  10579.856946 
##           192           193           194           195           196 
##  -1395.983206  -2224.330043 -10087.718172  -6231.985216   1331.358096 
##           197           198           199           200           201 
##  -5126.979866  -9713.519847   5423.777627  -2990.345824  -1643.143838 
##           202           203           204           205           206 
##   -735.780080   6566.123352   9989.871348    731.673614   3074.204093 
##           207           208           209           210           211 
##   3255.503128   5949.815760  13018.675649  -5452.255045 -11104.362486 
##           212           213           214           215           216 
##  -5537.953882 -10486.850646  -5021.494742   1566.237166 -12952.293357 
##           217           218           219           220           221 
##  16395.746213   7907.588381   1664.547242  26828.371876  12783.585462 
##           222           223           224           225           226 
##   7628.121728  14327.295643  -3575.771418  -1452.842369   4033.002927 
##           227           228           229           230           231 
##    613.624689   2983.055823   9238.941509   6092.969741  -1632.712891 
##           232           233           234           235           236 
##  -1582.483795   9646.792846 -11256.417638  -7102.889575  -8402.984277 
##           237           238           239           240           241 
## -10005.893631   3129.169010   1427.503554  -8209.045606  -8935.432362 
##           242           243           244           245           246 
##   9116.435971  -7688.075003   2534.696539 -10232.348561  -4025.557035 
##           247           248           249           250           251 
##   1444.852931   1044.774470 -12259.344790   3651.830606   2102.113300 
##           252           253           254           255           256 
##   4270.803257   2219.913094  -1062.425097  11233.568959  21025.845356 
##           257           258           259           260           261 
##   3425.284692  -4048.340972   4295.855270  -1502.943264   3908.290586 
##           262           263           264           265           266 
##  -4674.608641 -10748.521911  -4637.137059   -449.380986  -5114.206795 
##           267           268           269           270           271 
##   8833.241726  -4182.657659   4268.038893  -2009.015751   4518.747722 
##           272           273           274           275           276 
##    815.429201   7409.531046  -1278.779526  12145.192125  -4422.302098 
##           277           278           279           280           281 
##   1855.717094   -242.871690   7971.835903  -4912.337137  -2615.070415 
##           282           283           284           285           286 
## -11159.920171  -2612.202012  18707.271440   7912.155638   2885.014186 
##           287           288           289           290           291 
##   -478.049951   1043.655161   6530.639410   7030.587134 -18609.157162 
##           292           293           294           295           296 
## -11053.775061  -8072.809228   9694.223974   3147.731937  -1088.313633 
##           297           298           299           300           301 
##  27490.396455  10249.698831   5105.378421   9721.852408   3075.799004 
##           302           303           304           305           306 
##   -822.920997   8084.640413 -24094.733911  -3428.884355    -79.710113 
##           307           308           309           310           311 
##  -6870.037103  -3893.053639   3005.094189  -9101.676188  -3162.807643 
##           312           313           314           315           316 
##  -8118.377881   1618.730283  -3079.550866   2120.043647  -3995.013215 
##           317           318           319           320           321 
##  27526.519773   -571.628830   3432.426055  10974.593906   5758.781489 
##           322           323           324           325           326 
##  32555.925191   5374.660940 -20681.032861   1963.210079   1277.500401 
##           327           328           329           330           331 
##  -6303.120418  -1598.879104 -33140.038811    947.110836  -2214.518297 
##           332           333           334           335           336 
##      4.385033  -3055.784260   4201.022436   -299.068316  -6808.313040 
##           337           338           339           340           341 
##  -2984.533780  -2058.805509  -7543.260845   3976.148371  -1226.359310 
##           342           343           344           345           346 
##  -1589.811379   -844.092730    329.938541    640.751749  -1454.277850 
##           347           348           349           350           351 
##  -9284.042407 -13069.675343   2422.558611  -4192.979292  -3530.302031 
##           352           353           354           355           356 
##  -5851.456086   1873.536648   1521.235833   2899.825527  -3609.391800 
##           357           358           359           360           361 
##   -366.847703    828.638428   7169.085097    449.735015    133.400239 
##           362           363           364           365           366 
##   2752.692462  -2576.141585   -710.939743  -8578.339477  -4477.887312 
##           367           368           369           370           371 
##  -6068.374796  -4811.770369  -7117.008255   5143.752951    518.777411 
##           372           373           374           375           376 
##   7272.921765  -7465.002573  -2106.653905  -3231.658133  -2312.835202 
##           377           378           379           380           381 
## -12302.776731   2036.981218 -10486.933501   5827.716806   9487.886604 
##           382           383           384           385           386 
##   3300.059735  -2219.988648   1776.001242   6917.156752  11593.898167 
##           387           388           389           390           391 
##  -5604.560725  -5190.250829     -3.106167   8714.378182   1979.388188 
##           392           393           394           395           396 
##  11382.804832  -9703.919901   2917.050627    856.239458    702.565342 
##           397           398           399           400           401 
##   -516.616097   -430.544793 -14358.020760   8630.441910  -1046.664302 
##           402           403           404           405           406 
##  -1236.664208   7118.746911  -7780.308159  -1157.232016  -2388.193427 
##           407           408           409           410           411 
##  -5673.733024  -2716.128550  -3770.456423  -8607.088129   6275.022004 
##           412           413           414           415           416 
##   1800.624045  -7206.728291  -7534.647853  14371.633509   3992.803108 
##           417           418           419           420           421 
##   4669.780274  -7856.492220  -4580.918798  -2442.527119   2979.077072 
##           422           423           424           425           426 
## -13842.931292  -2643.173433  -8946.470892   3158.623603   7136.398250 
##           427           428           429           430           431 
##   6749.934437  -3802.743580  -3943.767352  -4550.454074  -1622.513710 
##           432           433           434           435           436 
##  -5543.531463  -6463.680944  -5792.200644  -1238.932861   -688.207488 
##           437           438           439           440           441 
##  -4810.522269   2743.328642   5011.030163  -4874.270261  -1983.128703 
##           442           443           444           445           446 
##   1750.785075  -3657.190382   3010.911476  -6395.407252 -11937.616970 
##           447           448           449           450           451 
##  -4355.769469   9800.919672  -1850.911361   4935.025320  -5677.219621 
##           452           453           454           455           456 
##   -939.830232    567.798627   3213.800187 -12073.462764   3541.609030 
##           457           458           459           460           461 
##  -6517.374840   6696.313017   3203.378650   2704.922246  -3643.572383 
##           462           463           464           465           466 
##   2286.551199    190.831664   1990.533624   -320.938985   3548.379842 
##           467           468           469           470           471 
##  -2435.907220   6002.008495  -6734.216914  -2770.456729  -2014.597221 
##           472           473           474           475           476 
##  -4473.838928   3181.122225   7992.013257  -5805.587446   1683.161607 
##           477           478           479           480           481 
##  -5975.350164  -2651.840845   2202.367976 -12731.786170  -9581.582391 
##           482           483           484           485           486 
##  -1042.587135    186.682887   -787.751824  -1162.878770  -9402.821076 
##           487           488           489           490           491 
##  11261.990652   6433.109001   7633.009604  -5209.705325   5579.270026 
##           492           493           494           495           496 
##   9514.837789   6291.230649 -13230.535039 -10357.505494  -3254.107067 
##           497           498           499           500           501 
##   -921.186282   -336.693000  -7434.573760    791.196583   4476.110643 
##           502           503           504           505           506 
##   5711.379699    877.015868    298.265346  -7021.619522    771.849236 
##           507           508           509           510           511 
##  -4842.344256   2029.938387  -1091.339990  -7953.223746   -412.664460 
##           512           513           514           515           516 
##  -2481.099665   -395.589689   1527.847339  -9292.566393  -7580.733830 
##           517           518           519           520           521 
##  24459.667471  10058.760030   6129.972852  -5080.140066   3033.606197 
##           522           523           524           525           526 
##  17255.051657  11739.588526 -23871.034603  -4847.632778  -3533.434221 
##           527           528           529           530           531 
##   4764.949718   -152.303664 -10899.380771   4571.379490  14103.760721 
##           532           533           534           535           536 
##  -4751.477497   4583.596107   5771.949666  -1565.489875  -4324.891845 
##           537           538           539           540           541 
##  -6870.435625  -1911.555930   8510.587482    336.479083  -7932.616666 
##           542           543           544           545           546 
##   2007.239037   -401.165238    565.389192 -10829.038910 -10881.743647 
##           547           548           549           550           551 
##   2202.801906   7179.364685  -1119.803869   1034.839595  -7517.990956 
##           552           553           554           555           556 
##   8753.244814   1118.215073 -11730.566416   9349.744445   8868.542476 
##           557           558           559           560           561 
##    327.066401   5076.793694  -3344.376789  14325.520988  21742.007521 
##           562           563           564           565           566 
##  -6187.605822  -9431.658393   6993.480191    454.291224   3676.793809 
##           567           568           569           570           571 
##  -7152.974735 -17103.668751   6832.086957   6626.552107   2111.757544 
##           572           573           574           575           576 
##   3312.262301   1990.216841  -1942.416714  14933.045087  -9403.254408 
##           577           578 
##  -6027.742974   8912.641780 
## 
## $fitted.values
##        2        3        4        5        6        7        8        9 
## 17283.00 20112.60 24340.31 24060.02 26398.86 23747.98 24459.87 19721.43 
##       10       11       12       13       14       15       16       17 
## 19459.61 16818.78 17591.45 14341.04 14391.89 15052.44 16738.54 15068.93 
##       18       19       20       21       22       23       24       25 
## 16097.21 15474.61 22513.32 21602.99 21086.06 22964.40 22294.54 22943.06 
##       26       27       28       29       30       31       32       33 
## 24777.62 18743.50 20459.00 28247.92 28305.22 27980.16 25624.20 27017.98 
##       34       35       36       37       38       39       40       41 
## 30837.29 31183.56 32584.03 30110.01 34108.20 37296.47 34370.76 31201.34 
##       42       43       44       45       46       47       48       49 
## 30056.23 20693.94 28166.23 30587.67 31670.15 38466.02 37963.72 42597.12 
##       50       51       52       53       54       55       56       57 
## 46807.22 39550.23 34152.23 29205.67 22392.44 28643.60 25245.35 21565.82 
##       58       59       60       61       62       63       64       65 
## 25946.95 27198.72 27493.90 27903.18 23799.61 40288.42 42120.99 37396.31 
##       66       67       68       69       70       71       72       73 
## 41577.27 46462.56 57064.84 55090.88 40425.22 37946.48 40945.74 35281.09 
##       74       75       76       77       78       79       80       81 
## 30761.43 21522.29 24703.99 20654.87 22728.05 17655.11 19658.09 18889.39 
##       82       83       84       85       86       87       88       89 
## 17923.13 16003.99 17273.86 20859.63 25250.07 26233.82 26258.63 26871.55 
##       90       91       92       93       94       95       96       97 
## 30971.15 29808.92 30802.10 28878.50 28083.26 28449.13 28853.20 22470.33 
##       98       99      100      101      102      103      104      105 
## 25466.74 18540.27 17403.47 15456.30 15754.20 16427.40 20872.41 19961.58 
##      106      107      108      109      110      111      112      113 
## 23450.98 23242.25 24908.01 27767.00 25280.58 21746.03 22019.75 24649.67 
##      114      115      116      117      118      119      120      121 
## 35447.03 33651.05 35477.17 38456.96 40400.78 38103.50 32956.52 29320.52 
##      122      123      124      125      126      127      128      129 
## 31390.47 29681.06 30855.31 38408.10 38052.77 37120.11 34003.03 35773.22 
##      130      131      132      133      134      135      136      137 
## 41137.73 40581.49 31837.58 33094.56 36264.60 32697.43 31094.68 30185.34 
##      138      139      140      141      142      143      144      145 
## 26763.65 28169.33 27940.39 25640.99 27652.90 26286.16 19927.84 22939.63 
##      146      147      148      149      150      151      152      153 
## 20779.76 23738.47 24275.12 25855.67 26036.62 27680.56 28973.25 31986.65 
##      154      155      156      157      158      159      160      161 
## 27484.85 26752.36 24322.74 30180.37 41363.67 39704.24 37085.48 42074.70 
##      162      163      164      165      166      167      168      169 
## 43474.55 46869.33 42363.30 37699.52 43091.20 59292.52 61490.96 59914.87 
##      170      171      172      173      174      175      176      177 
## 56760.21 55169.36 57855.21 56885.35 49225.15 52031.19 55750.65 55786.62 
##      178      179      180      181      182      183      184      185 
## 62871.34 53432.41 50203.45 41068.99 32660.69 36147.24 46188.19 45647.57 
##      186      187      188      189      190      191      192      193 
## 51563.49 57272.92 67989.17 73239.34 66973.05 67165.29 74191.84 69895.04 
##      194      195      196      197      198      199      200      201 
## 65445.58 54755.99 48823.07 50238.55 45860.52 38077.79 44462.77 42701.14 
##      202      203      204      205      206      207      208      209 
## 42341.35 42816.73 49568.70 58402.90 58034.80 59748.93 61394.47 65162.18 
##      210      211      212      213      214      215      216      217 
## 74570.11 66701.93 54964.10 49606.28 40658.35 37634.91 40729.29 30811.25 
##      218      219      220      221      222      223      224      225 
## 47679.70 54955.17 55851.49 78475.99 85924.59 87915.42 95459.77 86466.70 
##      226      227      228      229      230      231      232      233 
## 80502.28 80086.80 76757.52 75924.20 80631.89 81987.71 76457.63 71700.21 
##      234      235      236      237      238      239      240      241 
## 77318.85 64049.32 56135.13 48135.61 39799.12 43965.07 46104.47 39595.72 
##      242      243      244      245      246      247      248      249 
## 33314.42 43533.22 37815.73 41727.06 34038.84 32752.72 36385.37 39191.77 
##      250      251      252      253      254      255      256      257 
## 30078.03 35979.32 39757.20 44919.80 47621.28 47117.00 57354.15 74743.00 
##      258      259      260      261      262      263      264      265 
## 74559.20 67911.29 69383.94 65628.14 67065.32 60861.66 50202.71 46254.67 
##      266      267      268      269      270      271      272      273 
## 46462.78 42593.62 51343.23 47639.39 51760.44 49888.68 53930.86 54225.04 
##      274      275      276      277      278      279      280      281 
## 60205.21 57854.09 67467.16 61429.57 61638.30 59997.59 65704.91 59474.21 
##      282      283      284      285      286      287      288      289 
## 56059.35 45676.34 44083.01 61208.56 66704.41 67111.34 64544.92 63637.93 
##      290      291      292      293      294      295      296      297 
## 67614.13 71500.16 52614.35 42777.67 36825.78 47083.27 50305.03 49424.46 
##      298      299      300      301      302      303      304      305 
## 73471.02 79379.62 80043.15 84627.06 82836.78 77897.79 81343.16 56397.31 
##      306      307      308      309      310      311      312      313 
## 52681.57 52363.32 46191.91 43418.62 46999.68 39597.95 38327.95 32923.13 
##      314      315      316      317      318      319      320      321 
## 36684.27 35870.67 39678.44 37675.34 63302.20 61156.72 62770.26 70718.93 
##      322      323      324      325      326      327      328      329 
## 73091.50 98415.62 96803.32 72782.93 71588.21 69955.69 61957.16 59097.18 
##      330      331      332      333      334      335      336      337 
## 29231.32 32896.09 33332.90 35638.50 34983.41 40714.78 41783.74 37060.68 
##      338      339      340      341      342      343      344      345 
## 36279.95 36405.83 31753.71 37715.65 38374.95 38631.81 39502.20 41277.11 
##      346      347      348      349      350      351      352      353 
## 43087.85 42841.04 35829.25 26455.30 31766.98 30635.02 30227.60 27858.75 
##      354      355      356      357      358      359      360      361 
## 32508.76 36239.89 40675.96 38876.13 40128.65 42253.91 49603.55 50150.74 
##      362      363      364      365      366      367      368      369 
## 50351.16 52799.14 50298.08 49746.05 42436.60 39650.66 35851.20 33643.58 
##      370      371      372      373      374      375      376      377 
## 29725.68 36968.65 39241.51 47078.43 41087.23 40537.80 39084.12 38619.78 
##      378      379      380      381      382      383      384      385 
## 29543.73 34113.50 27208.00 35376.68 45646.08 49189.56 47473.57 49452.99 
##      386      387      388      389      390      391      392      393 
## 55634.82 65061.85 58314.97 52817.25 52547.62 59881.75 60401.91 69017.21 
##      394      395      396      397      398      399      400      401 
## 58189.95 59747.19 59310.01 58797.04 57293.26 56062.45 42902.56 51435.38 
##      402      403      404      405      406      407      408      409 
## 50441.95 49414.54 55776.45 48364.80 47680.19 46017.16 41720.99 40558.88 
##      410      411      412      413      414      415      416      417 
## 38634.66 32765.12 40589.52 43497.87 38202.93 33321.37 48101.63 51922.79 
##      418      419      420      421      422      423      424      425 
## 55827.92 48343.35 44689.24 43373.35 46937.79 35428.03 35158.90 29452.95 
##      426      427      428      429      430      431      432      433 
## 35008.46 43284.92 50134.74 46920.05 44006.74 40950.80 40839.67 37339.11 
##      434      435      436      437      438      439      440      441 
## 33501.20 30752.22 32318.64 34156.67 32173.53 37009.83 43177.27 39949.56 
##      442      443      444      445      446      447      448      449 
## 39657.36 42645.33 40544.37 44509.41 39785.47 30872.77 29717.37 41004.63 
##      450      451      452      453      454      455      456      457 
## 40688.12 46304.65 41967.54 42315.06 43925.63 47621.03 37557.39 42376.95 
##      458      459      460      461      462      463      464      465 
## 37828.26 45350.91 48849.36 51453.86 48203.45 50529.88 50730.18 52466.51 
##      466      467      468      469      470      471      472      473 
## 51967.19 54892.91 52237.56 57257.79 50559.03 48184.60 46779.41 43424.45 
##      474      475      476      477      478      479      480      481 
## 47157.56 54575.16 49036.27 50729.06 45549.84 43938.77 46754.36 36233.44 
##      482      483      484      485      486      487      488      489 
## 29834.44 31692.32 34372.47 35853.31 36813.25 30493.01 42946.46 49565.85 
##      490      491      492      493      494      495      496      497 
## 56354.28 51098.16 55901.59 63488.48 67276.54 53617.08 44252.68 42289.76 
##      498      499      500      501      502      503      504      505 
## 42610.98 43397.29 37917.80 40302.03 45571.05 51217.84 51923.16 52033.05 
##      506      507      508      509      510      511      512      513 
## 45773.58 47105.34 43387.49 46126.05 45793.80 39548.09 40672.24 39852.45 
##      514      515      516      517      518      519      520      521 
## 40951.30 43575.14 36459.16 31767.48 55510.67 63621.31 67251.85 60671.54 
##      522      523      524      525      526      527      528      529 
## 62002.81 75505.13 82439.03 57542.92 52444.43 49159.05 53511.16 53020.52 
##      530      531      532      533      534      535      536      537 
## 43264.33 48225.52 60808.33 55362.83 58739.62 62702.92 59773.61 54834.86 
##      538      539      540      541      542      543      544      545 
## 48337.27 47001.41 54889.81 54641.76 47247.48 49457.45 49285.18 49974.75 
##      546      547      548      549      550      551      552      553 
## 40681.17 32567.06 36882.21 44948.95 44747.16 46442.56 40489.18 49446.78 
##      554      555      556      557      558      559      560      561 
## 50594.99 40436.97 49919.31 57733.79 57102.63 60678.23 56471.48 68159.71 
##      562      563      564      565      566      567      568      569 
## 84745.75 74897.66 63531.52 67923.57 66059.49 67238.83 58860.67 42948.20 
##      570      571      572      573      574      575      576      577 
## 49913.73 55782.53 56958.02 59020.78 59663.85 56807.95 68979.25 58418.03 
##      578 
## 52179.64 
## 
## $shapiro.test
## [1] 0
## 
## $levenes.test
## [1] 0
## 
## $autcorr
## [1] "No autocorrelation evidence"
## 
## $post_sums
## [1] "Post-Est Warning"
## 
## $adjr_sq
## [1] 0.838
## 
## $fstat.bootstrap
## 
## ORDINARY NONPARAMETRIC BOOTSTRAP
## 
## 
## Call:
## boot::boot(data = x, statistic = f.stat, R = Reps, formula = depvar ~ 
##     ., parallel = parr)
## 
## 
## Bootstrap Statistics :
##        original     bias    std. error
## t1*    6.491232  0.5423541    3.314553
## t2* 1740.011791 24.2354545  222.479633
## WARNING: All values of t3* are NA
## 
## $itsa.plot
## 
## $booted.ints
##       Parameter    Lower CI Median F-value   Upper CI
## 1 interrupt_var    2.407072       6.553431   13.23165
## 2    lag_depvar 1419.801858    1754.111818 2151.39942

Ahora con las tendencias descompuestas

require(zoo)
require(scales)
Gastos_casa %>%
    dplyr::mutate(fecha= lubridate::parse_date_time(fecha, c("%d/%m/%Y"),exact=T)) %>% 
    dplyr::mutate(fecha2=strftime(fecha, format = "%Y-W%V")) %>%
    dplyr::mutate(gastador=ifelse(gastador=="Andrés",1,0)) %>%
    dplyr::mutate(treat=ifelse(fecha2>"2019-W26",1,0)) %>% 
   dplyr::mutate(gasto= dplyr::case_when(gasto=="Gas"~"Gas/Bencina",
    gasto=="aspiradora"~"electrodomésticos/mantención casa",
                                            gasto=="Plata fiestas patrias basureros"~"donaciones/regalos",
                                            gasto=="Tina"~"electrodomésticos/mantención casa",
                                            gasto=="Nexium"~"Farmacia",
                                            gasto=="donaciones"~"donaciones/regalos",
                                            gasto=="Regalo chocolates"~"donaciones/regalos",
                                            gasto=="filtro piscina msp"~"electrodomésticos/mantención casa",
                                            gasto=="Chromecast"~"electrodomésticos/mantención casa",
                                            gasto=="Muebles ratan"~"electrodomésticos/mantención casa",
                                            gasto=="Vacuna Influenza"~"Farmacia",
                                            gasto=="Easy"~"electrodomésticos/mantención casa",
                                            gasto=="Sopapo"~"electrodomésticos/mantención casa",
                                            gasto=="filtro agua"~"electrodomésticos/mantención casa",
                                            gasto=="ropa tami"~"donaciones/regalos",
                                            gasto=="yaz"~"Farmacia",
                                            gasto=="Yaz"~"Farmacia",
                                            gasto=="Remedio"~"Farmacia",
                                            gasto=="Entel"~"VTR",
                                            gasto=="Kerosen"~"Gas/Bencina",
                                            gasto=="Parafina"~"Gas/Bencina",
                                            gasto=="Plata basurero"~"donaciones/regalos",
                                            gasto=="Matri Andrés Kogan"~"donaciones/regalos",
                                            gasto=="Wild Protein"~"Comida",
                                            gasto=="Granola Wild Foods"~"Comida",
                                            gasto=="uber"~"Transporte",
                                            gasto=="Uber Reñaca"~"Transporte",
                                            gasto=="filtro piscina mspa"~"electrodomésticos/mantención casa",
                                            gasto=="Limpieza Alfombra"~"electrodomésticos/mantención casa",
                                            gasto=="Aspiradora"~"electrodomésticos/mantención casa",
                                            gasto=="Limpieza alfombras"~"electrodomésticos/mantención casa",
                                            gasto=="Pila estufa"~"electrodomésticos/mantención casa",
                                            gasto=="Reloj"~"electrodomésticos/mantención casa",
                                            gasto=="Arreglo"~"electrodomésticos/mantención casa",
                                            gasto=="Pan Pepperino"~"Comida",
                                            gasto=="Cookidoo"~"Comida",
                                            gasto=="remedios"~"Farmacia",
                                            gasto=="Bendina Reñaca"~"Gas/Bencina",
                                            gasto=="Bencina Reñaca"~"Gas/Bencina",
                                            gasto=="Vacunas Influenza"~"Farmacia",
                                            gasto=="Remedios"~"Farmacia",
                                            gasto=="Plata fiestas patrias basureros"~"donaciones/regalos",
                                        T~gasto)) %>% 
    dplyr::group_by(gastador, fecha,gasto, .drop=F) %>%
    #dplyr::mutate(fecha_simp=week(parse_date(fecha))) %>% 
#    dplyr::mutate(fecha_simp=tsibble::yearweek(fecha)) %>%#después de  diosi. Junio 24, 2019   
    dplyr::summarise(monto=sum(monto)) %>% 
    dplyr::mutate(gastador_nombre=plyr::revalue(as.character(gastador), c("0" = "Tami", "1"="Andrés"))) %>% 
  ggplot2::ggplot(aes(x = fecha, y = monto, color=as.factor(gastador_nombre))) +
  #stat_summary(geom = "line", fun.y = median, size = 1, alpha=0.5, aes(color="blue")) +
  geom_line(size=1) +
  facet_grid(gasto~.)+
  #geom_text(aes(x = fech_ing_qrt, y = perc_dup-0.05, label = paste0(n)), vjust = -1,hjust = 0, angle=45, size=3) +

  geom_vline(xintercept = as.Date("2019-06-24"),linetype = "dashed") +
  labs(y="Gastos (en miles)",x="Semanas y Meses", subtitle="Interlineado, incorporación de la Diosi; Azul= Tami; Rojo= Andrés") +
  ggtitle( "Figura 6. Gastos Semanales por Gastador e ítem (media)") +
  scale_y_continuous(labels = f <- function(x) paste0(x/1000)) + 
  scale_color_manual(name = "Gastador", values= c("blue", "red"), labels = c("Tami", "Andrés")) +
  scale_x_yearweek(breaks = "1 month", minor_breaks = "1 week", labels=date_format("%m/%y")) +
  guides(color = F)+
  sjPlot::theme_sjplot2() +
  theme(axis.text.x = element_text(vjust = 0.5,angle = 35)) +
  theme(
    panel.border = element_blank(), 
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(), 
    axis.line = element_line(colour = "black")
    )

autoplot(forecast::mstl(Gastos_casa$monto, lambda = "auto",iterate=5000000,start = 
lubridate::decimal_date(as.Date("2019-03-03"))))

 # scale_x_continuous(breaks = seq(0,400,by=30))
msts <- forecast::msts(Gastos_casa$monto,seasonal.periods = c(7,30.5,365.25),start = 
lubridate::decimal_date(as.Date("2019-03-03")))
#tbats <- forecast::tbats(msts,use.trend = FALSE)
#plot(tbats, main="Multiple Season Decomposition")
library(bsts)
library(CausalImpact)
ts_week_covid<-  
Gastos_casa %>%
    dplyr::mutate(fecha= lubridate::parse_date_time(fecha, c("%d/%m/%Y"),exact=T)) %>% 
    dplyr::mutate(fecha_week=strftime(fecha, format = "%Y-W%V")) %>%
    dplyr::mutate(day=as.Date(as.character(lubridate::floor_date(fecha, "day"))))%>%
    dplyr::group_by(fecha_week)%>%
    dplyr::summarise(gasto_total=sum(monto,na.rm=T)/1000,min_day=min(day))%>%
    dplyr::ungroup() %>% 
    dplyr::mutate(covid=dplyr::case_when(min_day>=as.Date("2020-03-17")~1,TRUE~0))%>%
    dplyr::mutate(covid=as.factor(covid))%>%
    data.frame()


ts_week_covid$gasto_total_na<-ts_week_covid$gasto_total
post_resp<-ts_week_covid$gasto_total[which(ts_week_covid$covid==1)]
ts_week_covid$gasto_total_na[which(ts_week_covid$covid==1)]<-NA
ts_week_covid$gasto_total[which(ts_week_covid$covid==0)]
##  [1]  98.357   4.780  56.784  50.506  64.483  67.248  49.299  35.786  58.503
## [10]  64.083  20.148  73.476 127.004  81.551  69.599 134.446  58.936  26.145
## [19] 129.927 104.989 130.860  81.893  95.697  64.579 303.471 151.106  49.275
## [28]  76.293  33.940  83.071 119.512  20.942  58.055  71.728  44.090  33.740
## [37]  59.264  77.410  60.831  63.376  48.754 235.284  29.604 115.143  72.419
## [46]   5.980  80.063 149.178  69.918 107.601  72.724  63.203  99.681 130.309
## [55] 195.898 112.066
# Model 1
ssd <- list()
# Local trend, weekly-seasonal #https://qastack.mx/stats/209426/predictions-from-bsts-model-in-r-are-failing-completely - PUSE UN GENERALIZED LOCAL TREND
ssd <- AddLocalLevel(ssd, ts_week_covid$gasto_total_na) #AddSemilocalLinearTrend #AddLocalLevel
# Add weekly seasonal
ssd <- AddSeasonal(ssd, ts_week_covid$gasto_total_na,nseasons=5, season.duration = 52) #weeks OJO, ESTOS NO SON WEEKS VERDADEROS. PORQUE TENGO MAS DE EUN AÑO
ssd <- AddSeasonal(ssd, ts_week_covid$gasto_total_na, nseasons = 12, season.duration =4) #years
# For example, to add a day-of-week component to data with daily granularity, use model.args = list(nseasons = 7, season.duration = 1). To add a day-of-week component to data with hourly granularity, set model.args = list(nseasons = 7, season.duration = 24).
model1d1 <- bsts(ts_week_covid$gasto_total_na, 
               state.specification = ssd, #A list with elements created by AddLocalLinearTrend, AddSeasonal, and similar functions for adding components of state. See the help page for state.specification.
               family ="student", #A Bayesian Analysis of Time-Series Event Count Data. POISSON NO SE PUEDE OCUPAR
               niter = 20000, 
               #burn = 200, #http://finzi.psych.upenn.edu/library/bsts/html/SuggestBurn.html Suggest the size of an MCMC burn in sample as a proportion of the total run.
               seed= 2125)
## =-=-=-=-= Iteration 0 Mon May 01 00:41:16 2023
##  =-=-=-=-=
## =-=-=-=-= Iteration 2000 Mon May 01 00:41:24 2023
##  =-=-=-=-=
## =-=-=-=-= Iteration 4000 Mon May 01 00:41:32 2023
##  =-=-=-=-=
## =-=-=-=-= Iteration 6000 Mon May 01 00:41:40 2023
##  =-=-=-=-=
## =-=-=-=-= Iteration 8000 Mon May 01 00:41:48 2023
##  =-=-=-=-=
## =-=-=-=-= Iteration 10000 Mon May 01 00:41:57 2023
##  =-=-=-=-=
## =-=-=-=-= Iteration 12000 Mon May 01 00:42:05 2023
##  =-=-=-=-=
## =-=-=-=-= Iteration 14000 Mon May 01 00:42:13 2023
##  =-=-=-=-=
## =-=-=-=-= Iteration 16000 Mon May 01 00:42:21 2023
##  =-=-=-=-=
## =-=-=-=-= Iteration 18000 Mon May 01 00:42:29 2023
##  =-=-=-=-=
#,
#               dynamic.regression=T)
#plot(model1d1, main = "Model 1")
#plot(model1d1, "components")

impact2d1 <- CausalImpact(bsts.model = model1d1,
                       post.period.response = post_resp)
plot(impact2d1)+
xlab("Date")+
  ylab("Monto Semanal (En miles)")

burn1d1 <- SuggestBurn(0.1, model1d1)
corpus <- Corpus(VectorSource(Gastos_casa$obs)) # formato de texto
d  <- tm_map(corpus, tolower)
d  <- tm_map(d, stripWhitespace)
d <- tm_map(d, removePunctuation)
d <- tm_map(d, removeNumbers)
d <- tm_map(d, removeWords, stopwords("spanish"))
d <- tm_map(d, removeWords, "menos")
tdm <- TermDocumentMatrix(d)
m <- as.matrix(tdm) #lo vuelve una matriz
v <- sort(rowSums(m),decreasing=TRUE) #lo ordena y suma
df <- data.frame(word = names(v),freq=v) # lo nombra y le da formato de data.frame
#findFreqTerms(tdm)
#require(devtools)
#install_github("lchiffon/wordcloud2")
#wordcloud2::wordcloud2(v, size=1.2)
wordcloud(words = df$word, freq = df$freq, 
          max.words=100, random.order=FALSE, rot.per=0.35, 
          colors=brewer.pal(8, "Dark2"), main="Figura 7. Nube de Palabras, Observaciones")

fit_month_gasto <- Gastos_casa %>%
    dplyr::mutate(fecha= lubridate::parse_date_time(fecha, c("%d/%m/%Y"),exact=T)) %>% 
    dplyr::mutate(fecha_month=strftime(fecha, format = "%Y-%m")) %>%
    dplyr::mutate(day=as.Date(as.character(lubridate::floor_date(fecha, "day"))))%>%
  dplyr::mutate(gasto2= dplyr::case_when(gasto=="Gas"~"Gas/Bencina",
    gasto=="aspiradora"~"electrodomésticos/mantención casa",
                                            gasto=="Plata fiestas patrias basureros"~"donaciones/regalos",
                                            gasto=="Tina"~"Electrodomésticos/ Mantención casa",
                                            gasto=="Nexium"~"Farmacia",
                                            gasto=="donaciones"~"donaciones/regalos",
                                            gasto=="Regalo chocolates"~"donaciones/regalos",
                                            gasto=="filtro piscina msp"~"Electrodomésticos/ Mantención casa",
                                            gasto=="Chromecast"~"Electrodomésticos/ Mantención casa",
                                            gasto=="Muebles ratan"~"Electrodomésticos/ Mantención casa",
                                            gasto=="Vacuna Influenza"~"Farmacia",
                                            gasto=="Easy"~"Electrodomésticos/ Mantención casa",
                                            gasto=="Sopapo"~"Electrodomésticos/ Mantención casa",
                                            gasto=="filtro agua"~"Electrodomésticos/ Mantención casa",
                                            gasto=="ropa tami"~"donaciones/regalos",
                                            gasto=="yaz"~"Farmacia",
                                            gasto=="Yaz"~"Farmacia",
                                            gasto=="Remedio"~"Farmacia",
                                            gasto=="Entel"~"VTR",
                                            gasto=="Kerosen"~"Gas/Bencina",
                                            gasto=="Parafina"~"Gas/Bencina",
                                            gasto=="Plata basurero"~"donaciones/regalos",
                                            gasto=="Matri Andrés Kogan"~"donaciones/regalos",
                                            gasto=="Wild Protein"~"Comida",
                                            gasto=="Granola Wild Foods"~"Comida",
                                            gasto=="uber"~"Otros",
                                            gasto=="Uber Reñaca"~"Otros",
                                            gasto=="filtro piscina mspa"~"Electrodomésticos/ Mantención casa",
                                            gasto=="Limpieza Alfombra"~"Electrodomésticos/ Mantención casa",
                                            gasto=="Aspiradora"~"Electrodomésticos/ Mantención casa",
                                            gasto=="Limpieza alfombras"~"Electrodomésticos/ Mantención casa",
                                            gasto=="Pila estufa"~"Electrodomésticos/ Mantención casa",
                                            gasto=="Reloj"~"Electrodomésticos/ Mantención casa",
                                            gasto=="Arreglo"~"Electrodomésticos/ Mantención casa",
                                            gasto=="Pan Pepperino"~"Comida",
                                            gasto=="Cookidoo"~"Comida",
                                            gasto=="remedios"~"Farmacia",
                                            gasto=="Bendina Reñaca"~"Gas/Bencina",
                                            gasto=="Bencina Reñaca"~"Gas/Bencina",
                                            gasto=="Vacunas Influenza"~"Farmacia",
                                            gasto=="Remedios"~"Farmacia",
                                            gasto=="Plata fiestas patrias basureros"~"donaciones/regalos",
                                        T~gasto)) %>% 
  dplyr::mutate(fecha_month=factor(fecha_month, levels=format(seq(from = as.Date("2019-03-03"), to = as.Date(substr(Sys.time(),1,10)), by = "1 month"),"%Y-%m")))%>% 
  dplyr::mutate(gasto2=factor(gasto2, levels=c("Agua", "Comida", "Comunicaciones","Electricidad", "Enceres", "Farmacia", "Gas/Bencina", "Diosi", "donaciones/regalos", "Electrodomésticos/ Mantención casa", "VTR", "Netflix", "Otros")))%>% 
    dplyr::group_by(fecha_month, gasto2, .drop=F)%>%
    dplyr::summarise(gasto_total=sum(monto, na.rm = T)/1000)%>%
  data.frame() %>% na.omit()

fit_month_gasto_23<-
fit_month_gasto %>% 
    #dplyr::filter()
    dplyr::filter(grepl("2023",fecha_month)) %>% 
    #sacar el ultimo mes
    dplyr::filter(as.character(format(as.Date(substr(Sys.time(),1,10)),"%Y-%m"))!=fecha_month) %>% 
    dplyr::group_by(gasto2) %>% 
    dplyr::summarise(gasto_prom=mean(gasto_total, na.rm=T)) %>% 
  data.frame()%>% ungroup()

fit_month_gasto_22<-
fit_month_gasto %>% 
    #dplyr::filter()
    dplyr::filter(grepl("2022",fecha_month)) %>% 
    #sacar el ultimo mes
    dplyr::filter(as.character(format(as.Date(substr(Sys.time(),1,10)),"%Y-%m"))!=fecha_month) %>% 
    dplyr::group_by(gasto2) %>% 
    dplyr::summarise(gasto_prom=mean(gasto_total, na.rm=T)) %>% 
  data.frame()%>% ungroup()

fit_month_gasto_21<-
fit_month_gasto %>% 
    #dplyr::filter()
    dplyr::filter(grepl("2021|2022",fecha_month)) %>% 
    #sacar el ultimo mes
    dplyr::filter(as.character(format(as.Date(substr(Sys.time(),1,10)),"%Y-%m"))!=fecha_month) %>% 
    dplyr::group_by(gasto2) %>% 
    dplyr::summarise(gasto_prom=mean(gasto_total, na.rm=T)) %>% 
  data.frame()%>% ungroup()


fit_month_gasto_20<-
fit_month_gasto %>% 
    #dplyr::filter()
    dplyr::filter(grepl("202",fecha_month)) %>% 
    #sacar el ultimo mes
    dplyr::filter(as.character(format(as.Date(substr(Sys.time(),1,10)),"%Y-%m"))!=fecha_month) %>% 
    dplyr::group_by(gasto2) %>% 
    dplyr::summarise(gasto_prom=mean(gasto_total, na.rm=T)) %>% 
  data.frame() %>% ungroup()

fit_month_gasto_23 %>% 
dplyr::right_join(fit_month_gasto_22,by="gasto2") %>%
dplyr::right_join(fit_month_gasto_21,by="gasto2") %>% 
dplyr::right_join(fit_month_gasto_20,by="gasto2") %>% 
  janitor::adorn_totals() %>% 
  #dplyr::select(-3)%>% 
  knitr::kable(format = "markdown", size=12, col.names= c("Item","2023","2022","2021","2020"))
Item 2023 2022 2021 2020
Agua 0.0000 5.410333 5.629750 6.359175
Comida 370.2575 310.278417 314.087500 343.358350
Comunicaciones 0.0000 0.000000 0.000000 0.000000
Electricidad 31.5895 47.072333 38.297667 32.318925
Enceres 33.2250 20.086417 17.443792 25.492375
Farmacia 4.9950 1.831667 7.913875 9.458850
Gas/Bencina 44.0600 44.325000 28.954333 26.956100
Diosi 12.1450 31.180667 41.934250 37.511450
donaciones/regalos 0.0000 0.000000 7.170083 6.867975
Electrodomésticos/ Mantención casa 0.0000 3.944000 30.269500 20.736700
VTR 10.9950 25.156667 22.121792 20.106700
Netflix 5.6450 7.151583 7.090167 7.292775
Otros 0.0000 3.151083 1.575542 0.945325
Total 512.9120 499.588167 522.488250 537.404700
## Joining with `by = join_by(word)`


2. UF Proyectada

Saqué la UF proyectada

#options(max.print=5000)

uf18 <-rvest::read_html("https://www.sii.cl/valores_y_fechas/uf/uf2018.htm")%>% rvest::html_nodes("table")
uf19 <-rvest::read_html("https://www.sii.cl/valores_y_fechas/uf/uf2019.htm")%>% rvest::html_nodes("table")
uf20 <-rvest::read_html("https://www.sii.cl/valores_y_fechas/uf/uf2020.htm")%>% rvest::html_nodes("table")
uf21 <-rvest::read_html("https://www.sii.cl/valores_y_fechas/uf/uf2021.htm")%>% rvest::html_nodes("table")
uf22 <-rvest::read_html("https://www.sii.cl/valores_y_fechas/uf/uf2022.htm")%>% rvest::html_nodes("table")

tryCatch(uf23 <-rvest::read_html("https://www.sii.cl/valores_y_fechas/uf/uf2023.htm")%>% rvest::html_nodes("table"),
    error = function(c) {
      uf23b <<- cbind.data.frame(Día=NA, variable=NA, value=NA)
      
    }
  )

tryCatch(uf23 <-uf23[[length(uf23)]] %>% rvest::html_table() %>% data.frame() %>% reshape2::melt(id.vars=1),
    error = function(c) {
      uf23 <<- cbind.data.frame(Día=NA, variable=NA, value=NA)
    }
)

uf_serie<-
bind_rows(
cbind.data.frame(anio= 2018, uf18[[length(uf18)]] %>% rvest::html_table() %>% data.frame() %>% reshape2::melt(id.vars=1)),

cbind.data.frame(anio= 2019, uf19[[length(uf19)]] %>% rvest::html_table() %>% data.frame() %>% reshape2::melt(id.vars=1)),

cbind.data.frame(anio= 2020, uf20[[length(uf20)]] %>% rvest::html_table() %>% data.frame() %>% reshape2::melt(id.vars=1)),

cbind.data.frame(anio= 2021, uf21[[length(uf21)]] %>% rvest::html_table() %>% data.frame() %>% reshape2::melt(id.vars=1)),

cbind.data.frame(anio= 2022, uf22[[length(uf22)]] %>% rvest::html_table() %>% data.frame() %>% reshape2::melt(id.vars=1)),
cbind.data.frame(anio= 2023, uf23)
)

uf_serie_corrected<-
uf_serie %>% 
dplyr::mutate(month=plyr::revalue(tolower(.[[3]]),c("ene" = 1, "feb"=2, "mar"=3, "abr"=4, "may"=5, "jun"=6, "jul"=7, "ago"=8, "sep"=9, "oct"=10, "nov"=11, "dic"=12))) %>% 
  dplyr::mutate(value=stringr::str_trim(value), value= sub("\\.","",value),value= as.numeric(sub("\\,",".",value))) %>% 
  dplyr::mutate(date=paste0(sprintf("%02d", .[[2]])," ",sprintf("%02d",as.numeric(month)),", ",.[[1]]), date3=lubridate::parse_date_time(date,c("%d %m, %Y"),exact=T),date2=date3) %>% 
   na.omit()#%>%  dplyr::filter(is.na(date3))
## Warning: There was 1 warning in `dplyr::mutate()`.
## i In argument: `date3 = lubridate::parse_date_time(date, c("%d %m, %Y"), exact
##   = T)`.
## Caused by warning:
## !  41 failed to parse.
#Day of the month as decimal number (1–31), with a leading space for a single-digit number.
#Abbreviated month name in the current locale on this platform. (Also matches full name on input: in some locales there are no abbreviations of names.)

warning(paste0("number of observations:",nrow(uf_serie_corrected),",  min uf: ",min(uf_serie_corrected$value),",  min date: ",min(uf_serie_corrected $date3 )))
## Warning: number of observations:1955, min uf: 26799.01, min date: 2018-01-01
# 
# uf_proyectado <- readxl::read_excel("uf_proyectado.xlsx") %>% dplyr::arrange(Período) %>% 
#   dplyr::mutate(Período= as.Date(lubridate::parse_date_time(Período, c("%Y-%m-%d"),exact=T)))

ts_uf_proy<-
ts(data = uf_serie_corrected$value, 
   start = as.numeric(as.Date("2018-01-01")), 
   end = as.numeric(as.Date(uf_serie_corrected$date3[length(uf_serie_corrected$date3)])), frequency = 1,
   deltat = 1, ts.eps = getOption("ts.eps"))

fit_tbats <- forecast::tbats(ts_uf_proy)


fr_fit_tbats<-forecast::forecast(fit_tbats, h=298)

La proyección de la UF a 298 días más 2023-05-09 00:04:58 sería de: 37.333 pesos// Percentil 95% más alto proyectado: 40.851,84

Ahora con un modelo ARIMA automático


arima_optimal_uf = forecast::auto.arima(ts_uf_proy)

  autoplotly::autoplotly(forecast::forecast(arima_optimal_uf, h=298), ts.colour = "darkred",
           predict.colour = "blue", predict.linetype = "dashed")%>% 
  plotly::layout(showlegend = F, 
          yaxis = list(title = "Gastos"),
         xaxis = list(
    title="Fecha",
      ticktext = as.list(seq(from = as.Date("2018-01-01"), 
                                  to = as.Date("2018-01-01")+length(fit_tbats$fitted.values)+298, by = 90)), 
      tickvals = as.list(seq(from = as.numeric(as.Date("2018-01-01")), 
                             to = as.numeric(as.Date("2018-01-01"))+length(fit_tbats$fitted.values)+298, by = 90)),
      tickmode = "array",
    tickangle = 90
    ))
fr_fit_tbats_uf<-forecast::forecast(arima_optimal_uf, h=298)
dplyr::group_by(reshape2::melt(data.frame(fr_fit_tbats)),variable) %>% dplyr::summarise(max=max(value)) %>% 
dplyr::right_join(dplyr::group_by(reshape2::melt(data.frame(fr_fit_tbats_uf)),variable) %>% dplyr::summarise(max=max(value)),by="variable") %>% 
  dplyr::mutate(variable=factor(variable,levels=c("Lo.95","Lo.80","Point.Forecast","Hi.80","Hi.95"))) %>% 
  dplyr::arrange(variable) %>% 
  knitr::kable(format="markdown", caption="Tabla. Estimación UF (de aquí a 298 días) según cálculos de gastos mensuales",
               col.names= c("Item","UF Proyectada (TBATS)","UF Proyectada (ARIMA)"))
## No id variables; using all as measure variables
## No id variables; using all as measure variables
Tabla. Estimación UF (de aquí a 298 días) según cálculos de gastos mensuales
Item UF Proyectada (TBATS) UF Proyectada (ARIMA)
Lo.95 36124.97 36115.65
Lo.80 36265.61 36337.69
Point.Forecast 37332.60 39863.15
Hi.80 39275.87 44647.56
Hi.95 40345.18 47180.28


3. Gastos proyectados

Lo haré en base a 2 cálculos: el gasto semanal y el gasto mensual en base a mis gastos desde marzo de 2019. La primera proyección la hice añadiendo el precio del arriendo mensual y partiendo en 2 (porque es con yo y Tami). No se incluye el último mes.

Gastos_casa_nvo <- readr::read_csv(as.character(path_sec),
                               col_names = c("Tiempo", "gasto", "fecha", "obs", "monto", "gastador",
                                             "link"),skip=1) %>% 
              dplyr::mutate(fecha= lubridate::parse_date_time(fecha, c("%d/%m/%Y"),exact=T)) %>% 
              dplyr::mutate(fecha_month=strftime(fecha, format = "%Y-%m")) %>%
              dplyr::mutate(day=as.Date(as.character(lubridate::floor_date(fecha, "day"))))
Gastos_casa_m <-
Gastos_casa_nvo %>% dplyr::group_by(fecha_month)%>%
              dplyr::summarise(gasto_total=(sum(monto)+500000)/1000,fecha=first(fecha))%>%
              data.frame()

uf_serie_corrected_m <-
uf_serie_corrected %>% dplyr::mutate(ano_m=paste0(anio,"-",sprintf("%02d",as.numeric(month)))) %>%  dplyr::group_by(ano_m)%>%
              dplyr::summarise(uf=(mean(value))/1000,fecha=first(date3))%>%
              data.frame() %>% 
  dplyr::filter(fecha>="2019-02-28")
#Error: Error in standardise_path(file) : object 'enlace_gastos' not found

ts_uf_serie_corrected_m<-
ts(data = uf_serie_corrected_m$uf[-length(uf_serie_corrected_m$uf)], 
   start = 1, 
   end = nrow(uf_serie_corrected_m), 
   frequency = 1,
   deltat = 1, ts.eps = getOption("ts.eps"))

ts_gastos_casa_m<-
ts(data = Gastos_casa_m$gasto_total[-length(Gastos_casa_m$gasto_total)], 
   start = 1, 
   end = nrow(Gastos_casa_m), 
   frequency = 1,
   deltat = 1, ts.eps = getOption("ts.eps"))

fit_tbats_m <- forecast::tbats(ts_gastos_casa_m)

seq_dates<-format(seq(as.Date("2019/03/01"), by = "month", length = dim(Gastos_casa_m)[1]+12), "%m\n'%y")

autplo2t<-
  autoplotly::autoplotly(forecast::forecast(fit_tbats_m, h=12), ts.colour = "darkred",
           predict.colour = "blue", predict.linetype = "dashed")%>% 
  plotly::layout(showlegend = F, 
          yaxis = list(title = "Gastos (en miles)"),
         xaxis = list(
    title="Fecha",
      ticktext = as.list(seq_dates[seq(from = 1, to = (dim(Gastos_casa_m)[1]+12), by = 3)]), 
      tickvals = as.list(seq(from = 1, to = (dim(Gastos_casa_m)[1]+12), by = 3)),
      tickmode = "array"#"array"
    )) 

autplo2t

Ahora asumiendo un modelo ARIMA, e incluimos como regresor al precio de la UF.

paste0("Optimo pero sin regresor")
## [1] "Optimo pero sin regresor"
arima_optimal = forecast::auto.arima(ts_gastos_casa_m)
arima_optimal
## Series: ts_gastos_casa_m 
## ARIMA(1,0,0) with non-zero mean 
## 
## Coefficients:
##          ar1      mean
##       0.2715  1006.339
## s.e.  0.1419    32.068
## 
## sigma^2 = 28637:  log likelihood = -326.53
## AIC=659.05   AICc=659.57   BIC=664.79
paste0("Optimo pero con regresor")
## [1] "Optimo pero con regresor"
arima_optimal2 = forecast::auto.arima(ts_gastos_casa_m, xreg=as.numeric(ts_uf_serie_corrected_m[1:(length(Gastos_casa_m$gasto_total))]))
arima_optimal2
## Series: ts_gastos_casa_m 
## Regression with ARIMA(1,0,0) errors 
## 
## Coefficients:
##          ar1  intercept     xreg
##       0.2392   627.1565  12.4935
## s.e.  0.1450   365.6547  11.9910
## 
## sigma^2 = 28663:  log likelihood = -326.01
## AIC=660.03   AICc=660.92   BIC=667.68
forecast_uf<-
cbind.data.frame(fecha=as.Date(seq(as.numeric(as.Date(uf_serie_corrected$date3[length(uf_serie_corrected$date3)])),(as.numeric(as.Date(uf_serie_corrected$date3[length(uf_serie_corrected$date3)]))+299),by=1), origin = "1970-01-01"),forecast::forecast(fit_tbats, h=300)) %>% 
  dplyr::mutate(ano_m=stringr::str_extract(fecha,".{7}")) %>% 
  dplyr::group_by(ano_m)%>%
              dplyr::summarise(uf=(mean(`Hi 95`,na.rm=T))/1000,fecha=first(fecha))%>%
            data.frame()
autplo2t2<-
  autoplotly::autoplotly(forecast::forecast(arima_optimal2,xreg=c(forecast_uf$uf[1],forecast_uf$uf), h=12), ts.colour = "darkred",
           predict.colour = "blue", predict.linetype = "dashed")%>% 
  plotly::layout(showlegend = F, 
          yaxis = list(title = "Gastos (en miles)"),
         xaxis = list(
    title="Fecha",
      ticktext = as.list(seq_dates[seq(from = 1, to = (dim(Gastos_casa_m)[1]+12), by = 3)]), 
      tickvals = as.list(seq(from = 1, to = (dim(Gastos_casa_m)[1]+12), by = 3)),
      tickmode = "array"#"array"
    )) 

autplo2t2
fr_fit_tbats_m<-forecast::forecast(fit_tbats_m, h=12)
fr_fit_tbats_m2<-forecast::forecast(arima_optimal, h=12)
fr_fit_tbats_m3<-forecast::forecast(arima_optimal2, h=12,xreg=c(forecast_uf$uf[1],forecast_uf$uf))

dplyr::right_join(dplyr::group_by(reshape2::melt(data.frame(fr_fit_tbats_m3)),variable) %>% dplyr::summarise(max=max(value)), dplyr::group_by(reshape2::melt(data.frame(fr_fit_tbats_m2)),variable) %>% dplyr::summarise(max=max(value)),by="variable") %>% 
dplyr::right_join(dplyr::group_by(reshape2::melt(data.frame(fr_fit_tbats_m)),variable) %>% dplyr::summarise(max=max(value)),by="variable") %>% 
  dplyr::mutate(variable=factor(variable,levels=c("Lo.95","Lo.80","Point.Forecast","Hi.80","Hi.95"))) %>% 
  dplyr::arrange(variable) %>% 
  knitr::kable(format="markdown", caption="Estimación en miles de la plata a gastar en el futuro (de aquí a 12 meses) según cálculos de gastos mensuales",
               col.names= c("Item","Modelo ARIMA con regresor (UF)","Modelo ARIMA sin regresor","Modelo TBATS")) 
## No id variables; using all as measure variables
## No id variables; using all as measure variables
## No id variables; using all as measure variables
Estimación en miles de la plata a gastar en el futuro (de aquí a 12 meses) según cálculos de gastos mensuales
Item Modelo ARIMA con regresor (UF) Modelo ARIMA sin regresor Modelo TBATS
Lo.95 789.5902 661.7235 706.4674
Lo.80 907.8807 781.0071 820.3702
Point.Forecast 1131.3367 1006.3390 1035.5378
Hi.80 1354.7927 1231.6709 1293.3154
Hi.95 1473.0832 1350.9545 1429.7746


4. Gastos mensuales (resumen manual)

path_sec2<- paste0("https://docs.google.com/spreadsheets/d/",Sys.getenv("SUPERSECRET"),"/export?format=csv&id=",Sys.getenv("SUPERSECRET"),"&gid=847461368")

Gastos_casa_mensual_2022 <- readr::read_csv(as.character(path_sec2),
                #col_names = c("Tiempo", "gasto", "fecha", "obs", "monto", "gastador","link"),
                skip=0)
## Rows: 55 Columns: 4
## -- Column specification --------------------------------------------------------
## Delimiter: ","
## chr (1): mes_ano
## dbl (3): n, Tami, Andrés
## 
## i Use `spec()` to retrieve the full column specification for this data.
## i Specify the column types or set `show_col_types = FALSE` to quiet this message.
head(Gastos_casa_mensual_2022,5) %>% 
  knitr::kable("markdown",caption="Resumen mensual, primeras 5 observaciones")
Resumen mensual, primeras 5 observaciones
n mes_ano Tami Andrés
1 marzo_2019 175533 68268
2 abril_2019 152640 55031
3 mayo_2019 152985 192219
4 junio_2019 291067 84961
5 julio_2019 241389 205893


(
Gastos_casa_mensual_2022 %>% 
    reshape2::melt(id.var=c("n","mes_ano")) %>%
  dplyr::mutate(gastador=as.factor(variable)) %>% 
  dplyr::select(-variable) %>% 
 ggplot2::ggplot(aes(x = n, y = value, color=gastador)) +
  scale_color_manual(name="Gastador", values=c("red", "blue"))+
  geom_line(size=1) +
  #geom_vline(xintercept = as.Date("2019-06-24"),linetype = "dashed") +
  labs(y="Gastos (en miles)",x="Meses", subtitle="Azul= Tami; Rojo= Andrés") +
  ggtitle( "Gastos Mensuales (total manual)") +
  scale_y_continuous(labels = f <- function(x) paste0(x/1000)) + 
#  scale_color_manual(name = "Gastador", values= c("blue", "red"), labels = c("Tami", "Andrés")) +
#  scale_x_yearweek(breaks = "1 month", minor_breaks = "1 week", labels=date_format("%m/%y")) +
 # guides(color = F)+
  sjPlot::theme_sjplot2() +
  theme(axis.text.x = element_text(vjust = 0.5,angle = 35)) +
  theme(
    panel.border = element_blank(), 
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(), 
    axis.line = element_line(colour = "black")
    )
) %>% ggplotly()


Session Info

Sys.getenv("R_LIBS_USER")
## [1] "D:\\a\\_temp\\Library"
sessionInfo()
## R version 4.1.2 (2021-11-01)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows Server x64 (build 20348)
## 
## Matrix products: default
## 
## locale:
## [1] LC_COLLATE=Spanish_Chile.1252  LC_CTYPE=Spanish_Chile.1252   
## [3] LC_MONETARY=Spanish_Chile.1252 LC_NUMERIC=C                  
## [5] LC_TIME=Spanish_Chile.1252    
## 
## attached base packages:
## [1] grid      stats     graphics  grDevices utils     datasets  methods  
## [8] base     
## 
## other attached packages:
##  [1] CausalImpact_1.3.0  bsts_0.9.9          BoomSpikeSlab_1.2.5
##  [4] Boom_0.9.11         scales_1.2.1        ggiraph_0.8.7      
##  [7] tidytext_0.4.1      DT_0.27             autoplotly_0.1.4   
## [10] rvest_1.0.3         plotly_4.10.1       xts_0.13.1         
## [13] forecast_8.21       wordcloud_2.6       RColorBrewer_1.1-3 
## [16] SnowballC_0.7.1     tm_0.7-11           NLP_0.2-1          
## [19] tsibble_1.1.3       lubridate_1.9.2     forcats_1.0.0      
## [22] dplyr_1.1.2         purrr_1.0.1         tidyr_1.3.0        
## [25] tibble_3.2.1        ggplot2_3.4.2       tidyverse_2.0.0    
## [28] sjPlot_2.8.14       lattice_0.20-45     gridExtra_2.3      
## [31] plotrix_3.8-2       sparklyr_1.8.1      httr_1.4.5         
## [34] readxl_1.4.2        zoo_1.8-12          stringr_1.5.0      
## [37] stringi_1.7.12      DataExplorer_0.8.2  data.table_1.14.8  
## [40] reshape2_1.4.4      fUnitRoots_4021.80  plyr_1.8.8         
## [43] readr_2.1.4        
## 
## loaded via a namespace (and not attached):
##   [1] uuid_1.1-0          backports_1.4.1     systemfonts_1.0.4  
##   [4] selectr_0.4-2       igraph_1.4.2        lazyeval_0.2.2     
##   [7] splines_4.1.2       crosstalk_1.2.0     digest_0.6.31      
##  [10] htmltools_0.5.5     fansi_1.0.4         ggfortify_0.4.16   
##  [13] magrittr_2.0.3      tzdb_0.3.0          modelr_0.1.11      
##  [16] vroom_1.6.3         timechange_0.2.0    anytime_0.3.9      
##  [19] tseries_0.10-53     colorspace_2.1-0    xfun_0.39          
##  [22] crayon_1.5.2        jsonlite_1.8.4      lme4_1.1-33        
##  [25] glue_1.6.2          r2d3_0.2.6          gtable_0.3.3       
##  [28] emmeans_1.8.5       sjstats_0.18.2      sjmisc_2.8.9       
##  [31] car_3.1-2           quantmod_0.4.22     abind_1.4-5        
##  [34] mvtnorm_1.1-3       DBI_1.1.3           ggeffects_1.2.1    
##  [37] Rcpp_1.0.10         viridisLite_0.4.1   xtable_1.8-4       
##  [40] performance_0.10.3  bit_4.0.5           htmlwidgets_1.6.2  
##  [43] timeSeries_4021.105 gplots_3.1.3        ellipsis_0.3.2     
##  [46] spatial_7.3-14      farver_2.1.1        pkgconfig_2.0.3    
##  [49] nnet_7.3-16         sass_0.4.5          dbplyr_2.3.2       
##  [52] janitor_2.2.0       utf8_1.2.3          tidyselect_1.2.0   
##  [55] labeling_0.4.2      rlang_1.1.0         munsell_0.5.0      
##  [58] cellranger_1.1.0    tools_4.1.2         cachem_1.0.7       
##  [61] cli_3.6.1           generics_0.1.3      sjlabelled_1.2.0   
##  [64] broom_1.0.4         evaluate_0.20       fastmap_1.1.1      
##  [67] yaml_2.3.7          knitr_1.42          bit64_4.0.5        
##  [70] caTools_1.18.2      forge_0.2.0         nlme_3.1-153       
##  [73] slam_0.1-50         xml2_1.3.3          tokenizers_0.3.0   
##  [76] compiler_4.1.2      rstudioapi_0.14     curl_5.0.0         
##  [79] bslib_0.4.2         highr_0.10          fBasics_4022.94    
##  [82] Matrix_1.5-4        its.analysis_1.6.0  nloptr_2.0.3       
##  [85] urca_1.3-3          vctrs_0.6.1         pillar_1.9.0       
##  [88] lifecycle_1.0.3     networkD3_0.4       lmtest_0.9-40      
##  [91] jquerylib_0.1.4     estimability_1.4.1  bitops_1.0-7       
##  [94] insight_0.19.1      R6_2.5.1            KernSmooth_2.23-20 
##  [97] janeaustenr_1.0.0   codetools_0.2-18    gtools_3.9.4       
## [100] boot_1.3-28         MASS_7.3-54         assertthat_0.2.1   
## [103] rprojroot_2.0.3     withr_2.5.0         fracdiff_1.5-2     
## [106] bayestestR_0.13.1   parallel_4.1.2      hms_1.1.3          
## [109] quadprog_1.5-8      timeDate_4022.108   minqa_1.2.5        
## [112] snakecase_0.11.0    rmarkdown_2.21      carData_3.0-5      
## [115] TTR_0.24.3          base64enc_0.1-3
#save.image("__analisis.RData")

sesion_info <- devtools::session_info()
dplyr::select(
  tibble::as_tibble(sesion_info$packages),
  c(package, loadedversion, source)
) %>% 
  DT::datatable(filter = 'top', colnames = c('Row number' =1,'Variable' = 2, 'Percentage'= 3),
              caption = htmltools::tags$caption(
        style = 'caption-side: top; text-align: left;',
        '', htmltools::em('Packages')),
      options=list(
initComplete = htmlwidgets::JS(
        "function(settings, json) {",
        "$(this.api().tables().body()).css({
            'font-family': 'Helvetica Neue',
            'font-size': '50%', 
            'code-inline-font-size': '15%', 
            'white-space': 'nowrap',
            'line-height': '0.75em',
            'min-height': '0.5em'
            });",#;
        "}")))